Roland Pish
Roland Pish

Reputation: 815

AngularJS ng-bind once default value

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

Upvotes: 3

Views: 1600

Answers (1)

Alex Pollan
Alex Pollan

Reputation: 873

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

Upvotes: 2

Related Questions