javascript AngularJS 指令 - 编译不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20454987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:32:21  来源:igfitidea点击:

AngularJS Directive - compile is not working

javascriptangularjsangularjs-directive

提问by moustacheman

ng-clickis not working. Can someone help me, how to use the $compile?

ng-click不管用。有人可以帮助我,如何使用$compile

I have created a plunker http://plnkr.co/edit/AhxGYnOsJ7rPqcQquMfq?p=preview

我创建了一个 plunker http://plnkr.co/edit/AhxGYnOsJ7rPqcQquMfq?p=preview

// main.js
var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {

})
.directive('myDir', function($compile){
  return {
    restrict: 'CAE',
    link: function(scope, element, attrs){
      var dropdown = '';
      dropdown += '<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="'+ attrs.id +'">';
      dropdown += '<li role="presentation"><a href="javascript:void(0);" ng-click="alert(\'a\')">Actions</a></li>';
      dropdown += '</ul>';
      //dropdown = $compile(dropdown)(scope);
      element.after(dropdown);
    }
  }
});

回答by Chris Montgomery

I would pull out the click function into your controller. There is no alerton your controller's scope so it does nothing. Also, I try to avoid lots of nested quotes if possible. A simple and clean refactor.

我会把点击功能拉到你的控制器中。alert您的控制器范围内没有,因此它什么都不做。此外,如果可能,我会尽量避免使用大量嵌套引号。一个简单而干净的重构。

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.actionClick = function() {
        alert("a");
      }
})
.directive('myDir', function($compile){
  return {
    restrict: 'CAE',
    link: function(scope, element, attrs){
      var dropdown = '';
      dropdown += '<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="'+ attrs.id +'">';
      dropdown += '<li role="presentation"><a href="javascript:void(0);" ng-click="actionClick()">Actions</a></li>';
      dropdown += '</ul>';
      dropdown = $compile(dropdown)(scope);
      element.after(dropdown);
    }
  }
});

see plunkr

见 plunkr

回答by Maxim Shoustin

You were very close. Compile template with $compile:

你非常接近。编译模板$compile

var app = angular.module('myApp', ['ngGrid']);
app.controller('MyCtrl', function($scope) {
    $scope.doSomething = function(){
      alert('d');
    };
})
.directive('myDir', function($compile){
  return {
    restrict: 'CAE',
    link: function(scope, element, attrs){
      var dropdown = '';
      dropdown += '<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="'+ attrs.id +'">';
      dropdown += '<li role="presentation"><a href="#" ng-click="doSomething()">Actions</a></li>';
      dropdown += '</ul>';

      var a_input = angular.element($compile(dropdown)(scope));

      element.append(a_input);
    }
  }
});

Demo Plunker

演示 Plunker

comment

评论

AngularJS doesn't know about alert. If you want to call it from HTML, override it like @udidu showed.

AngularJS 不知道alert. 如果你想从 HTML 调用它,像@udidu 显示的那样覆盖它。

回答by udidu

When you're using ng-clickthe ng-click directive searche for the expression in the current scope. Since their is no alertfunction on the directive's scope (or it's parent scope) then nothing happen.

当您使用ng-clickng-click 指令时,请在当前范围内搜索表达式。由于它们alert在指令的作用域(或其父作用域)上没有任何功能,因此什么也不会发生。

You can add the alertfunction on your scope like so:

你可以alert像这样在你的范围内添加函数:

.directive('myDir', function($compile){
  return {
    restrict: 'CAE',
    link: function(scope, element, attrs){


          // add the alert function on the scope
          scope.alert = function(param) {
              alert(param);
          }


      var dropdown = '';
      dropdown += '<ul class="dropdown-menu pull-right" role="menu" aria-labelledby="'+ attrs.id +'">';
      dropdown += '<li role="presentation"><a href="javascript:void(0);" ng-click="alert(\'a\')">Actions</a></li>';
      dropdown += '</ul>';
      dropdown = $compile(dropdown)(scope);
      element.after(dropdown);
    }
  }
});