Javascript AngularJS:如何使用ng bind html将事件绑定到动态生成的html

Javascript AngularJS:如何使用ng bind html将事件绑定到动态生成的html,javascript,angularjs,Javascript,Angularjs,我正在编写一个自定义AngularJS指令。如何将事件绑定到由指令本身生成的动态生成的html。经过研究,我发现我们需要使用$watch和$compile,但我无法让它工作。我发现这与我的问题有关。在AngularJS中,jQuery的替代方法是什么 非常感谢您的帮助。请在中查看这个 app.directive('btn',['$sce',function($sce){ 返回{ 限制:'E', 替换:正确, 模板:“”, 链接:函数($scope,element,attrs){ $scope.b

我正在编写一个自定义AngularJS指令。如何将事件绑定到由指令本身生成的动态生成的html。经过研究,我发现我们需要使用$watch$compile,但我无法让它工作。我发现这与我的问题有关。在AngularJS中,jQuery的替代方法是什么

非常感谢您的帮助。请在中查看这个

app.directive('btn',['$sce',function($sce){
返回{
限制:'E',
替换:正确,
模板:“”,
链接:函数($scope,element,attrs){
$scope.buttonHtml=$sce.trustAsHtml('Click Me');
$scope.showMessage=函数(){
警惕(“你点击了我”);
}
} }; }]);

提前感谢

我希望这样做:

app.directive('yourcustomdirective', function(){
  return {
    restrict : "E", //<---E is directive like <customdirective></customdirective>
    replace : true,
    template:"<button>Click me.</button>", // <---dynamically generated button
    link:function(scope, element, attrs){
      element.on('click', function(){ // <-----click event bound 
        alert(scope.greet); // <----alerts the greet in the scope
      });
    }
  };
});
app.directive('yourcustomdirective',function(){
返回{

限制:“E”,我希望这样做:

app.directive('yourcustomdirective', function(){
  return {
    restrict : "E", //<---E is directive like <customdirective></customdirective>
    replace : true,
    template:"<button>Click me.</button>", // <---dynamically generated button
    link:function(scope, element, attrs){
      element.on('click', function(){ // <-----click event bound 
        alert(scope.greet); // <----alerts the greet in the scope
      });
    }
  };
});
app.directive('yourcustomdirective',function(){
返回{

限制:“E”,我希望这样做:

app.directive('yourcustomdirective', function(){
  return {
    restrict : "E", //<---E is directive like <customdirective></customdirective>
    replace : true,
    template:"<button>Click me.</button>", // <---dynamically generated button
    link:function(scope, element, attrs){
      element.on('click', function(){ // <-----click event bound 
        alert(scope.greet); // <----alerts the greet in the scope
      });
    }
  };
});
app.directive('yourcustomdirective',function(){
返回{

限制:“E”,我希望这样做:

app.directive('yourcustomdirective', function(){
  return {
    restrict : "E", //<---E is directive like <customdirective></customdirective>
    replace : true,
    template:"<button>Click me.</button>", // <---dynamically generated button
    link:function(scope, element, attrs){
      element.on('click', function(){ // <-----click event bound 
        alert(scope.greet); // <----alerts the greet in the scope
      });
    }
  };
});
app.directive('yourcustomdirective',function(){
返回{



限制:“E”,//您可以发布您已经使用$watch和$compile尝试过的代码吗?您无法使用这些代码吗?您可以展示到目前为止您所获得的,这会更容易帮助您。jqlite不是在为您做这些吗?@MichaelBromley请稍候。我将用代码编辑我的问题我的问题samples@vishnu如果你能发布你的代码,那将是我们很有意思。你可以发布你已经使用$watch和$compile尝试过的代码,但是你无法使用这些代码吗?你可以展示一下你到目前为止所得到的,这会更容易帮助你。jqlite不是在为你做这些吗?@MichaelBromley请稍候。我将用代码编辑我的问题我的问题samples@vishnu如果你能发布你的代码,那将是你的seful。你能发布你已经使用$watch和$compile尝试过的代码吗?你能展示一下你到目前为止的成果吗?这会更容易帮助你。jqlite不是在为你做这些吗?@MichaelBromley请稍候。我会用代码编辑我的问题我的问题samples@vishnu如果你能发布你的代码,那将是有用。你能发布你已经使用$watch和$compile尝试过的代码吗?你能展示一下你到目前为止的成果吗?这会更容易帮助你。jqlite不是在为你做这些吗?@MichaelBromley请稍候。我会用代码编辑我的问题我的问题samples@vishnu如果你能发布你的代码,那将是有用。我有一个与此按钮相关联的ng click。如何使该ng click工作。@毗瑟奴确实在控制器范围内创建了一个函数并访问它。谢谢你的帮助。我需要那个doGreet()函数也在指令内部,而不是在控制器中。问题是在使用ng bind html时。动态内容不会触发事件。请在plunker@vishnu中检查此问题。您在angularjs中有jqlite可用。您可以使用此
元素。查找('button')。打开('click',回调);
。我有一个与此按钮相关联的ng click。如何使该ng click工作。@毗瑟奴确实在控制器范围内创建了一个函数并访问它。谢谢你的帮助。我需要那个doGreet()函数也在指令内部,而不是在控制器中。问题是在使用ng bind html时。动态内容不会触发事件。请在plunker@vishnu中检查此问题。您在angularjs中有jqlite可用。您可以使用此
元素。查找('button')。打开('click',回调);
。我有一个与此按钮相关联的ng click。如何使该ng click工作。@毗瑟奴确实在控制器范围内创建了一个函数并访问它。谢谢你的帮助。我需要那个doGreet()函数也在指令内部,而不是在控制器中。问题是在使用ng bind html时。动态内容不会触发事件。请在plunker@vishnu中检查此问题。您在angularjs中有jqlite可用。您可以使用此
元素。查找('button')。打开('click',回调);
。我有一个与此按钮相关联的ng click。如何使该ng click工作。@毗瑟奴确实在控制器范围内创建了一个函数并访问它。谢谢你的帮助。我需要那个doGreet()函数也在指令内部,而不是在控制器中。问题是在使用ng bind html时。动态内容不会触发事件。请在plunker@vishnu中检查此问题。您在angularjs中有jqlite可用。您可以使用此
元素。查找('button')。打开('click',callback);