问题
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 require
s 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