在隔离范围指令观察中未观察到表单元素 ng-model 更改 [英] form element ng-model change not observered in isolate scope directive watch

查看:20
本文介绍了在隔离范围指令观察中未观察到表单元素 ng-model 更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布这个小提琴之前,我检查了类似的问题.得到的答案很少,但所有这些都不是表单元素.http://jsfiddle.net/dgQAd/

Before posting this fiddle, i checked SO for similar question. Got few answer but all those were not form elements. http://jsfiddle.net/dgQAd/

我有以下问题:

1) 文本框绑定到模型 uname,但加载时文本框未显示值.为什么会这样?

1) The textbox is bound to a model uname, but onload the textbox is not displaying the value. why this is happening?

2) 在为此寻找答案时,我看到了类似 require:ngModel 的内容,并在链接函数中注入了一个控制器,我如何在指令的链接函数中使用这个注入的控制器.

2)while searching for answers for this, i saw something like require:ngModel, and injecting a controller inside the linking function, how can i use this injected controller inside the linking function of the directive.

3)如何从隔离作用域指令的链接函数内部查找父作用域ng-model的变化.

3)How to look for the changes in the parent scope ng-model from inside a linking function of an isolate scope directive.

推荐答案

我能够让 ng-model 与隔离作用域一起工作的唯一方法是对隔离作用域属性使用相同的名称:范围:{uname":=ngModel"}.你的 $watch 现在可以工作了.有关更多信息,另请参见 https://stackoverflow.com/a/14792601/215945

The only way I've been able to get ng-model to work with an isolate scope is to use the same name for the isolate scope property: scope:{ "uname":"=ngModel" }. Your $watch will now work. For more on this see also https://stackoverflow.com/a/14792601/215945

当指令 require 是另一个指令的控制器时,该控制器可用作链接函数的第四个选项.在您的小提琴中,这就是您所说的 ngModel:

When a directive requires another directive's controller, that controller is available as the 4th option to the linking function. In your fiddle, that is what you called ngModel:

link:function(scope,el,attrs,ngModel){

通常,我更喜欢将这个命名为 ngModelCtrl 以提醒我它是一个控制器.

Normally, I prefer to name this ngModelCtrl to remind me that it is a controller.

$observe 仅用于使用@"语法的隔离作用域属性.

$observe is only used with isolate scope properties that use the '@' syntax.

这篇关于在隔离范围指令观察中未观察到表单元素 ng-model 更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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