简体   繁体   English

ui-如果与ng-model冲突?

[英]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 of ui-if , as angular does provide the same functionality. 我鼓励您使用ng-if而不是ui-if ,因为angular确实提供了相同的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM