Model does not update within ng-if

雨燕双飞 提交于 2019-12-05 01:03:54

ng-if has its own scope, so you need to use:

<br/><button ng-click="$parent.foo = true;">Show foo</button>

Updated fiddle: http://jsfiddle.net/78R52/1/

Ah, ng-if creates a new scope! So, "there has to be a dot in the model name"!

http://jsfiddle.net/78R52/2/

As others have said, ng-if has its own scope. What i want to say, it's a bad practice to put expressions in the view. The good practice is to have a scope function that's called within the view.

var ctrl = function($scope){
$scope.foo = false;
$scope.fn = fn;
function fn(){
$scope.foo = true;
}
/////

<div ng-app ng-controller="ctrl">
  foo: {{foo}}
  <div ng-if="foo" style="background-color: #f00;">
    <p>foo</p>
  </div>
  <div ng-if="!foo">
    <br/><button ng-click="fn()">Show foo</button>
  </div>
  <button ng-click="fn()">Show foo</button>
</div>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!