AngularJS NG-如果和范围 [英] AngularJS ng-if and scopes

查看:221
本文介绍了AngularJS NG-如果和范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想了解NG-如果和范围。我所知,NG-如果创建一个新的子范围。这里是我的问题:

I am trying to understand ng-if and scopes. As I am aware, ng-if creates a new child scope. Here is my issue:

查看

<input ng-model="someValue1" />
<div ng-if="!someCondition">
    <input ng-model="$parent.someValue2" />
</div>

控制器

$scope.someCondition = true;

if ($scope.someCondition) {
    $scope.someValue2 = $scope.someValue1;        
}

如果someCondition设置为true,那么someValue2应该是一样的someValue1。

If someCondition is set to true, then someValue2 should be the same as someValue1.

我的问题是,我不能在这两种情况下(true或false)访问someValue2。我怎么能做到这一点?

My problem is that I can't access someValue2 in both situations (true or false). How could I achieve this?

推荐答案

是NG-如果创建一个新的子范围

yes ng-if creates a new child scope

要保持一个手表的型号在纳克如果拇指规则是:

to keep a watch on model in ng if the thumb rule is:

请勿使用范围为模型
例如。

ng-if='showStuff' //here my scope is model **INCORRECT**
ng-if='someObject.showStuff' // ** CORRECT **

在NG-模型中使用一个对象的属性那么即使NG-如果创建新的子范围
父作用域将有变化。

use an object property in ng-model then even if ng-if creates the new child scope parent scope will have the changes.

更多的工作plunker请按: http://jsfiddle.net/Erk4V/4/

more working plunker please follow : http://jsfiddle.net/Erk4V/4/

这篇关于AngularJS NG-如果和范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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