Javascript AngularJS绑定单击而不是ng单击

Javascript AngularJS绑定单击而不是ng单击,javascript,angularjs,Javascript,Angularjs,我在看AngularJs,有一个问题,这是我的指令: myApp.directive("enter", function(){ return{ restrict: 'A', scope:{}, controller: function($scope){ $scope.logSomething=function(somevalue){ console.log(somevalue+" is logged"); }

我在看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

myApp.directive(
    'clickMe',
    function () {
        return {
            template : '<div>Click me !</div>',
            replace : true,
            link : function (scope, element) {
                element.bind('click', function ()
                {
                    alert('Clicked !');
                });
            },
        };
    }
);
myApp.directive(
“点击我”,
函数(){
返回{
模板:“单击我!”,
替换:正确,
链接:功能(范围、元素){
元素绑定('单击',函数()
{
警报('Clicked!');
});
},
};
}
);

当然,在您的情况下,您必须使用
ngClick

试试这个:

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 );
       });
    }
}
});
myApp.directive(“enter”,function()){
返回{
限制:“A”,
作用域:{},
控制器:功能($scope){
$scope.logSomething=函数(somevalue){
log(somevalue+“被记录”);
}
},
模板:“”+
“点击我”
}
});
指令(“按钮”,函数(){
返回{
限制:“A”,
链接:功能(范围、元素){
元素绑定(“单击”,函数(e){
scope.logSomething(scope.myModel);
});
}
}
});

Plunk:

使用link:function(scope,element,attrs){element.bind(“click”,function(){scope.$apply(attrs.enter);})@Codezilla模板呢?对。对不起。所以元素不是我们想要的。你可以使用jquery访问目标div。这不理想。或者你可以在目标div.tnx上实现另一个指令,但为什么我要这样做“必须”使用ng click?在您的示例中我也看不到输入字段?此指令是“子指令”",它只替换了您显示的指令中的
div
。给出了一个更完整的示例。您必须使用
ngClick
,因为这是AngularJS的一个基本功能。尝试重新实现一个已经存在的功能是没有意义的。tnx所以我们需要两个指令?我想知道这是否可能实现一模一样directive@Spring似乎在一个指令中组合这样一个不同的functtonality并不是最佳做法。它肯定是不可重用的。如果愿意,您可以尝试另一种语法:正如注释中所建议的,您也可以通过jQuery方式访问div(例如,
元素.children('div').bind(…)
)。但这是一个坏主意,因为这里只有一条指令明显地混合了关注点。@Cherniv如果我将“单击我”放在html中而不是指令中,那还可以吗?不做任何更改else@Spring它们都在同一个作用域中,只是如果您没有像下面这样在“enter”目录中定义私有作用域:
scope:{}
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 );
       });
    }
}
});