Reputation: 18660
I have this HTML code:
<select class="state ng-scope ng-pristine ng-invalid ng-invalid-required">
<option value="" selected="selected" class="">Seleccione un estado</option>
</select>
And I wrote this directive:
app.directive('state', ['$http', function($http){
return {
restrict: 'C',
link: function(scope, element, attrs) {
console.log("element" + element);
console.log("attrs" + attrs);
}
}
}]);
Why directive isn't fired?
Upvotes: 0
Views: 74
Reputation: 17723
I would add a ng-model on your select and then add a watch in your directive. Pass your variable into the directive and output your desired result. Pseudo code:
<select ng-model="myList" class="ng-scope ng-pristine ng-invalid ng-invalid-required">
<option value="" selected="selected" class="">Seleccione un estado</option>
</select>
<span state selectedItem="myList"></span>
and your directive might look like:
directive('state', ['$http', function($http){
return {
restrict: 'A',
scope: {
myList: '=myList'
},
link: function(scope, element, attrs) {
scope.$watch('myList', function (newValue, oldValue) {
console.log(changed);
}
}
}
}])
Upvotes: 1
Reputation: 18660
The problem is I wasn't listen on change
event. The right code is:
link: function(scope, element, attrs) {
console.log(element);
...
}
Upvotes: 0