4

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.

Rajkamal Subramanian
  • 6,884
  • 4
  • 52
  • 69

1 Answers1

3

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.

Community
  • 1
  • 1
Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
  • if possible can you point out some docs that elaborates on `require:ngModel` usage. Thanks for your answer. – Rajkamal Subramanian Mar 03 '13 at 03:05
  • @rajkamal, see "Implementing custom form controls (using ngModel)" on the [forms page](http://docs.angularjs.org/guide/forms). See also http://stackoverflow.com/questions/14567908/watch-controller-model-value-from-inside-directive – Mark Rajcok Mar 03 '13 at 03:33