javascript - Select custom directive and ng-change - Stack Overflow

Using <select> with a custom directive, I want to run a function when the value is changed.html&l

Using <select> with a custom directive, I want to run a function when the value is changed.

html

<select name="user_type" ng-model="user_type" user-type ng-change="check_type()">

directive

gb_dash.directive('userType', function(){
    return{
        restrict: 'A',
        require: '^ngModel',
        scope:{
            check_type: '&'
        },
        link: function(scope, elem, attrs){
            scope.check_type = function(){
                console.log('changed');
            }
        }
    }
});

Using <select> with a custom directive, I want to run a function when the value is changed.

html

<select name="user_type" ng-model="user_type" user-type ng-change="check_type()">

directive

gb_dash.directive('userType', function(){
    return{
        restrict: 'A',
        require: '^ngModel',
        scope:{
            check_type: '&'
        },
        link: function(scope, elem, attrs){
            scope.check_type = function(){
                console.log('changed');
            }
        }
    }
});
Share Improve this question edited Jul 7, 2015 at 18:58 scniro 17k8 gold badges66 silver badges107 bronze badges asked Jul 7, 2015 at 18:28 Abel DAbel D 1,0801 gold badge19 silver badges30 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

Since you have an isolate scope, your check_type is not in the same scope as the ng-change expression. And you wouldn't want it to be so.

Instead, ng-model provides a way to register a listener with ngModel.$viewChangeListeners - which is exactly what ngChange directive uses - to hook into view model change events.

require: "ngModel",
scope: { }, // to mimic your directive - doesn't have to be isolate scope
link: function(scope, element, attrs, ngModel){
   ngModel.$viewChangeListeners.push(function(){
     console.log('changed');
   }
}

Because of your isolate scope, you can include a call to $parent to achieve your result. Try this change...

link: function(scope, elem, attrs) {
    scope.$parent.check_type = function() {
        console.log('changed');
    }
}

However, calling $parent may not be ideal for your situation.

Alternatively you can ditch ng-change and instead $watch your ngModel. This could be acplished as such...

link: function(scope, elem, attrs, ngModel) {
    scope.$watch(function() {
        return ngModel.$modelValue;
    }, function(newValue, oldValue) {
        console.log('model value changed...', newValue, oldValue);
    }, true);
}

JSFiddle Link - $parent demo

JSFiddle Link - $watch demo

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742281833a4414631.html

相关推荐

  • javascript - Select custom directive and ng-change - Stack Overflow

    Using <select> with a custom directive, I want to run a function when the value is changed.html&l

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信