ng-click not working with ng-if

Sebastian Barth picture Sebastian Barth · Nov 20, 2014 · Viewed 10.6k times · Source

Why does the second button not work, when ng-if is used?

I want to realize a button that is present only when the model value is set / not ""/ not null.

Template:

<input type="text" ng-model="blub"/>
<br/>
<button ng-click="blub = 'xxxx'">X</button>
<br/>
<button ng-click="blub = 'yyyy'" ng-if="blub.length">Y</button>

Controller:

angular.module('test', [])
.controller('Main', function ($scope) {
    // nothing to do here
});

To play around: JSFiddle

Answer

Muhammad Reda picture Muhammad Reda · Nov 20, 2014

Use ng-show Instead of ng-if. That should work.

Fiddle