Get value from ngModel in custom directive

后端 未结 2 1887
执笔经年
执笔经年 2021-01-24 09:46

I\'m trying to get the value from ngModel from my input through a directive, but I can\'t get the value.

Here is my code:

angular
    .module(\'myapp\')
         


        
2条回答
  •  -上瘾入骨i
    2021-01-24 10:25

    You're very close, but you need to make a minor adjustment to your directive in order to be able to pass and access the ng-model item.

    angular
        .module('myapp')
        .directive('infoVal', myVal);
    
    function myVal() {
        return {
            require: 'ngModel',
            link: function(scope, element, attrs, ngModel) {
                console.log('ng-model value', ngModel.$viewValue);
            }
        }
    };
    

提交回复
热议问题