58274

Get one-time binding to work for ng-if

This question has been asked before, but I can't get that solution working, so I'm wondering if there's something different in my specifics.

The code is really, really simple:

<h1 ng-if="vm.building.name !== undefined">update {{vm.building.name}}</h1>

As written, this snippet works fine. If vm.building.name is set, tag appears.

However, if I try to make the one-time binding work, that is:

<h1 ng-if="::vm.building.name !== undefined">update {{vm.building.name}}</h1>

The tag doesn't appear at all. I've tried variations:

<h1 ng-if="::(vm.building.name !== undefined)">update {{vm.building.name}}</h1>

Still nope. It's such a frustratingly simple thing. Hell, the code seems to work in a jsfiddle, but I can't get it working for my particular app. Is there a setting somewhere? Some random bit of trivia I need to change?

Is there I'm using angular 1.5.7, if that matters.

EDIT

<h1 ng-if="vm.building.name !== undefined">update {{::vm.building.name}}</h1>

That is, setting the {{::vm.building.name}} ultimately gives me the result I'm looking for, but at the cost of a additional, useless watcher, right?

Further edit on my edit: this isn't a great solution for a number of reasons.

EDIT 2

My problem stems from the fact that vm.building.name waits on an API call and therefore misses the first $watch digest (I think). So instead of "one-time" as defined by angular, is there a way to have ng-if do its thing directly after vm.building.name is set the first time and then quit?

Answer1:

Well, In here you're checking one-time data binding vm.building.name value with undefined and since it is one-time data binding this condition will always be false.

modify your ng-if and make it more simpler

<h1 ng-if="::vm.building.name">update {{::vm.building.name}}</h1>

Please find working plunker : https://plnkr.co/edit/QA6ZA7e0zHnN45oQWzQH?p=preview

This will work!

Cheers!

Recommend

  • replace letters in python string
  • GWT listbox - how to look up item index using text?
  • Get username of logged in user in os via browser
  • Is it safe to use operator != as operator < in a std::set?
  • Setting jsp checkbox with a value from database
  • Simple command-line app I/O in Dart
  • How to issue a command that produces infinite output and return immediately
  • Drawing a Path in XAML (with binding) vs. C#. One works, one doesn't
  • Consuming a WCF service in a Java Client using wsHttpBinding
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • How to autopopulate a field in SugarCRM form
  • Debugging VB6 Code From Visual Studio 2010
  • Differences in dis-assembled C code of GCC and Borland?
  • Force show.bind execution
  • Display images in Django
  • Allowing both email and username for authentication
  • Ajax Loaded meta Tags
  • Is there any way to access browser form field suggestions from JavaScript?
  • Volley JsonObjectRequest send headers in GET Request
  • Sony Xperia Z Tablet not found by adb
  • How to recover from a Spring Social ExpiredAuthorizationException
  • Does CUDA 5 support STL or THRUST inside the device code?
  • Importing jscolor library in angular 2
  • How do you troubleshoot character encoding problems?
  • How can I estimate amount of memory left with calling System.gc()?
  • QuartzCore.framework for Mono Develop
  • R: gsub and capture
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Comma separated Values
  • How to set the response of a form post action to a iframe source?
  • Hits per day in Google Big Query
  • Change div Background jquery
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • Qt: Run a script BEFORE make
  • How can I remove ASP.NET Designer.cs files?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • Easiest way to encapsulate a HTML5 webpage into an android app?
  • Django query for large number of relationships
  • reshape alternating columns in less time and using less memory
  • How to load view controller without button in storyboard?