AngularJS ngModelController [英] AngularJS ngModelController

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

问题描述

什么是检查ngModelController有效性的正确方式?

What is the correct way to check ngModelController validity?

我有一个有效的控制对象的指令内部。如果我从内部指令记录对象到控制台我得到:

I have a valid controller object inside of a directive. If I log the object to the console from inside the directive I get:

的console.log(CTRL)

$dirty: false
$invalid: true
$modelValue: ""
$name: undefined
$pristine: true
$valid: false
$viewValue: ""
...

那么,如果我问如果(Ctrl键。$有效===真)来记录对象再次控制台,它具有完全相同的输出一样。

then, if I ask if( ctrl.$valid === true ) to log the object to the console again, it does with the exact same output.

console.log(ctrl); //ctrl.$valid is false
if(ctrl.$valid == true) {
    console.log(ctrl); //ctrl.$valid is false
}

此外,如果我检查元素我可以看到相应的被应用NG-无效类。

additionally if I inspect the element I can see the the appropriate ng-invalid class is being applied.

我的尝试的制作plunkr证明,但我无法想象我将能够重复这一点。

I will try to make a plunkr to demonstrate but I can't imagine I would be able to duplicate this.

更新如果我的console.log(Ctrl键。$有效)是版画真正。所以,现在我明白了是怎么传递的条件,但不是为什么对象的形式显示 $有效

Update If I console.log(ctrl.$valid) is prints true. So now I get how it's passing the conditional but not why the object form shows $valid being false.

此外,我做了一个plnkr,它显示出我在做什么一个例子,但它的的距离有同样的问题。 例如

Also I made a plnkr that shows an example of what I am doing but it does not suffer from the same problem. example

推荐答案

你有没有尝试过这样的:

Have you tried this:

console.log(JSON.stringify(ctrl));
if(ctrl.$valid == true) {
    console.log(JSON.stringify(ctrl));
}

console.log("$valid is ", ctrl.$valid);
if(ctrl.$valid == true) {
    console.log("$valid should be true and is actually ", ctrl.$valid);
}

这可能与(旧)Chrome有错误。我相信,预期的行为是,当你展开控制台的对象,你在扩张时获得对象的价值,而不是在登录的时候,所以你需要打印的对象作为字符串或登录时克隆。

It could be related to this (old) Chrome bug. I believe the expected behavior is that when you expand the object in the console you get the value of the object at expansion time, not at log time, so you would need to print the object as a string or clone it when logging.

修改 - 更多的细节...

Edit - Some more details...

如果你运行这个 VAR OBJ = {A:1,B:{}};的console.log(OBJ); OBJ ['一'] = 2;的console.log(OBJ); 在Chrome控制台,你会看到这是输出:

If you run this var obj = {a: 1, b: {}}; console.log(obj); obj['a'] = 2; console.log(obj); in the Chrome console you will see this being output:

> Object {a: 1, b: Object}
  a: 2
  b: Object
  __proto__: Object

> Object {a: 2, b: Object}
  a: 2
  b: Object
  __proto__: Object

编辑2

希望这会回答你的问题。看来,你可能会增加一个解析器到 $解析器阵列的之后 $ setViewValue(...)被调用。其结果是,当你的指令首次初始化已添加不会被执行的任何验证。你可以通过调用类似 CTRL手动执行它们$ setViewValue(CTRL $ viewValue); 解析器已被添加之后

Hopefully this will answer your question. It appears that you may be adding a parser to the $parsers array after $setViewValue(...) has been called. As a result, when your directive is first initialized any validations you have added will not have been executed. You can manually execute them by calling something like ctrl.$setViewValue(ctrl.$viewValue); after your parser has been added.

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

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