模型不会在ng-if内更新 [英] Model does not update within ng-if

查看:92
本文介绍了模型不会在ng-if内更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在角应用程序中有一个奇怪的行为,我不知道这是一个错误还是一个已知的限制:

I've got a strange behavior in an angular application and I don't know if that's a bug or a known limitation:

'use strict';

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

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<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="foo = true;">Show foo</button>
  </div>
  <button ng-click="foo = true">Show foo</button>
</div>

http://jsfiddle.net/78R52/

我希望点击其中一个按钮设置 foo = true ,但是点击第一个按钮(在 ng-if =!foo)不会更改模型。

I would expect that clicking one of the buttons would set foo = true, but clicking the first button (within the ng-if="!foo") doesn't change the model.

测试版本 1.2.1

推荐答案

ng-if 有自己的范围,所以你需要使用:

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

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

更新小提琴: http://jsfiddle.net/78R52/1/

这篇关于模型不会在ng-if内更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆