Get value from ngModel in custom directive

后端 未结 2 1895
执笔经年
执笔经年 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条回答
  •  渐次进展
    2021-01-24 10:15

    By this sample you can get the first ngModel Value and also onChange Values

            var app = angular.module("app", []);
    
            app.controller("controller", function ($scope) {
    
                $scope.test = "hi";
    
            });
    
    
            app.directive("directiveName", function () {
                return {
                    restrict: "A",
                    replace: true,
                    scope: {
                        directiveName: "="
                    },
                    require: "^ngModel",
                    link: function (scope, element, attr, ngModel) {
                        var firstValue = scope.directiveName;
    
                        console.log("firstValue", firstValue);
    
    
                        element.on("input propertychange", function () {
                            console.log("onchange", ngModel.$viewValue);
                        });
    
                    }
                }
            })
    
    
    
        
    
    
    
        
        {{test}}
    
      
    
    

提交回复
热议问题