[英]ui-if clashing with ng-model?
Very simple code: 很简单的代码:
<div ui-if="showMemberId">
<input ng-model="memberId" ng-change="test()" id="memberId" name="memberId" class="text" type="text">
</div>
With this for the controller: 与此相关的控制器:
$scope.memberId = 'SDF';
$scope.test = function test(){
console.log($scope.memberId);
};
$scope.showMemberId = true;
Bizarrely, the ng-change
works but not the ng-model
. 奇怪的是,
ng-change
有效,但ng-model
不起作用。 When I type into the input, it just logs the string SDF
to the console over and over because the model isn't changing. 当我键入输入内容时,由于模型没有更改,它只是一遍又一遍地将字符串
SDF
登录到控制台。
Any ideas? 有任何想法吗?
Seems like ui-if
is creating a child scope like ng-if
does. 好像
ui-if
正在像ng-if
一样创建子范围。 So the outer memberId
scope will be different than ng-model
present inside the ui-if
. 因此,外部
memberId
范围将不同于ui-if
内部存在的ng-model
。 In such cases you need to follow the dot rule
while defining ng-model
. 在这种情况下,您需要在定义
ng-model
时遵循dot rule
。 Basically when it creates a child scope, its protypically inherited from parent scope. 基本上,当它创建一个子范围时,它典型地继承自父范围。 And
prototypal inheritance
works on object
type not on primitive types
prototypal inheritance
适用于object
类型,而不适用于primitive types
To make it working you need to define object inside controller like $scope.model ={}
& define all the property in it . 要使其正常工作,您需要在控制器内部定义对象,例如
$scope.model ={}
并定义其中的所有属性。
Markup 标记
<div ui-if="showMemberId">
<input ng-model="model.memberId" ng-change="test()"
id="memberId" name="memberId" class="text" type="text"/>
</div>
Controller 调节器
$scope.model = {}; //made an object type model
I'd encourage you to use
ng-if
instead ofui-if
, as angular does provide the same functionality.我鼓励您使用
ng-if
而不是ui-if
,因为angular确实提供了相同的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.