form element ng-model change not observered in isolate scope directive watch

南笙酒味 提交于 2019-12-10 15:54:49

问题


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

I have the following questions:

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

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)How to look for the changes in the parent scope ng-model from inside a linking function of an isolate scope directive.


回答1:


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

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){

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

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



来源:https://stackoverflow.com/questions/15179468/form-element-ng-model-change-not-observered-in-isolate-scope-directive-watch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!