问题描述:

I've been working on the directive 'input-container' where I need to add a $watch on the child's input ng-model. How am I supposed to do that?

<input-container>

<label>Doc</label>

<input type="text" ng-model="model.userName"/>

</input-container>

<input-container>

<label>Senha</label>

<input type="password" ng-model="model.password"/>

</input-container>

I need to add different classes to my directive wrap depending on my input value. I added a watch like this

app.directive('inputContainer', function () {

return {

restrict: 'E',

link: function (scope, elem, attrs) {

elem.addClass('input-container');

elem.find('input').bind('focus', function () {

elem.addClass('focused');

});

//---- code

scope.$watch(???????, function (value) {

if (value)

//-- do something

else

//-- do something

})

}

};});

However I don't know how to watch my input ngModel. This was the only way I've found to get this working. Anyway, it doesn't allow me to access $viewValue

 scope.$watch(elem.find('input')[0].attributes['ng-model'].nodeValue, function (value) {

if (value)

//-- do something

else

//-- do something

})

What I need is to get the input ngmodel variable dynamically, once it has different names for each directive instance, this way I could do something like this:

 scope.$watch(function(){

return childInputNgModel.$viewValue;

}, function (newValue) {

if (newValue)

//-- do something

else

//-- do something

})

相关阅读:
Top