3

I have a "view" page constructed with AngularJS where I display the value of a model as follows:

<span ng-bind="doctor.address || '[Not defined yet]'"></span>

If the value of doctor.address is undefined I correctly get the [Not defined yet] text.

Since this is a "view" page, I know the values won't change while it is displayed, so, I wanted to save some watchers and use the ng-bind once feature:

<span ng-bind="::doctor.address || '[Not defined yet]'"></span>

but if there is a valid an existing value for doctor.address I still get the [Not defined yet] text.

If I use the following:

<span ng-bind="::doctor.address"></span>

I correctly get the doctor address value.

This doesn't work either:

<span ng-bind="::doctor.address">[Not defined yet]</span>

So, how can I define a "default value" when using the ng-bind once feature?

EDIT: this is the actual code with a last test that didn't work:

...
Restangular.one('doctors', params.doctorId).get().then(function(response) {
  $scope.doctor = response;
  $scope.test = "Hello!!!";
}, function(errorResponse) {
  console.log(errorResponse);
});
...

and this is the markup:

<span ng-bind="::test || '[Not defined yet]'"></span>

and still no idea of what might be wrong (the async call would be affecting?)

This is a plunk (forked from the one on Alex Pollan answer) where I can reproduce the issue (I used a custom promise): https://plnkr.co/edit/pjPeTZq8GxaDfvEWk3xV?p=preview

4
  • Which version of angularjs are you using? Seems to work for 1.4.5 jsfiddle.net/3t4yyh7p/12 Commented Jan 8, 2016 at 13:40
  • @Nils, I'm using angularjs 1.4.4 Commented Jan 8, 2016 at 14:52
  • Seems to work with 1.4.4 as well jsfiddle.net/injulkarnilesh/3t4yyh7p/13 Commented Jan 8, 2016 at 15:00
  • @Nils, yes, you're right. A simple example like your fiddle works fine in my code. My $scope.doctor gets populated via Restangular and it contains the address. Odd. Commented Jan 8, 2016 at 15:35

1 Answer 1

2

Try:

<span ng-bind="::(doctor.address || '[Not defined yet]')"></span>

Hope it helps

UPDATE: It is funny because...

<span ng-bind="::doctor.address || '[Not defined yet]'"></span>

... works for me too (AngularJs v1.4.x)

Check: https://plnkr.co/edit/9jeau6x3pKdlggZ0DzO1?p=preview

Sign up to request clarification or add additional context in comments.

10 Comments

I will upgrade to angularjs 1.4.5 and test again.
I upgraded to 1.4.8 and I'm having the same problem. Testing with something like your plunk into my code worked great. So there might be something strange when $scope.doctor gets populated via Restangular because the address is there.
Do you have the value doctor.address defined somehow before Restangular set it? One time binding stop watching once the value becomes defined (!undefined).
silly question... Is response.address defined when you debug your code?
According to documentation: "One-time expressions will stop recalculating once they are stable, which happens after the first digest if the expression result is a non-undefined value."... and the literal '[Not defined yet]' makes the expression DEFINED. That's why it doesn't work as expected. If you remove that literal string it works fine (plnkr.co/edit/6rllFpIUuDIwWJoStxwj?p=preview).... I think to make it work with one time binding is not worth the effort and the result won't be better. My suggestion is to use the regular binding.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.