0

我试图访问范围的值,但它显示为未定义,我需要告诉我用户插入的值。范围未定义在指令

我的HTML

<div ng-app="myApp" ng-controller="myCtrl as model"> 
    <input type="text" ng-model="model.cero" ng-cero="">  
    </div> 

在JS

angular 
    .module("myApp",[]) 
    .controller('myCtrl', function($scope){ 
    var model=this; 
    }) 
    .directive ('ngCero', function($parse){ 
     var linkFunction =function(scope, element, attrs){ 
     element.bind("keypress", function(event) { 
     if(event.which === 44 || event.which === 13) { 
       console.log($parse(attrs.format)); 
      } 
     }); 
     }; 
     return{ 
     restrict : 'A', 
     } 
    }) 

回答

0

我解决

angular 
    .module("myApp",[]) 
    .controller('myCtrl', function($scope){ 
     var model=this; 
    }) 
    .directive ('ngCero', function($parse){ 
     var linkFunction =function(scope, element, attrs){ 
           element.bind("keypress", function(event) { 
           if(event.which === 44 || event.which === 13) { 
            console.log($parse(attrs.format)); 
           } 
           }); 
          }; 
     return{ 
      restrict : 'A', 
      scope:{ 
        cero:"ngModel" 
       }, 
      link: linkFunction 
     } 
})