Spr*_*ing 13 javascript angularjs
我正在看AngularJs并有一个问题,这是我的指示:
myApp.directive("enter", function(){
return{
    restrict: 'A',
    scope:{},
    controller: function($scope){
        $scope.logSomething=function(somevalue){
            console.log(somevalue+" is logged");
        }
    },
    template: '<input type="text" ng-model="myModel">'+
              '<div ng-click="logSomething(myModel)">click me</div>'
}
})
这有效,但我的问题是如何使用bind click而不是ng-click指令做同样的事情?并不是说它更好(也许?),但是好奇心
它应该包括这样的东西,但无法得到大局:
 function(scope, element, attrs){
    element.bind("click", function(){
        scope.$apply(attrs.enter);
    })
Che*_*niv 16
试试这个:
myApp.directive("enter", function(){
  return{
    restrict: 'A',
    scope:{},
    controller: function($scope){
        $scope.logSomething=function(somevalue){
            console.log(somevalue+" is logged");
        }
    },
    template: '<input type="text" ng-model="myModel">'+
              '<div button>click me</div>'
}
});
myApp.directive("button", function(){   
  return{
    restrict: 'A',
    link: function(scope , element){
       element.bind("click", function(e){
          scope.logSomething( scope.myModel );
       });
    }
}
});
Plunk:http://plnkr.co/edit/RCcrs5?p = preview
| 归档时间: | 
 | 
| 查看次数: | 43317 次 | 
| 最近记录: |