[英]AngularJS Directive - compile is not working
ng-click
is not working. ng-click
不起作用。 Can someone help me, how to use the $compile
? 有人可以帮我,如何使用
$compile
吗?
I have created a plunker http://plnkr.co/edit/AhxGYnOsJ7rPqcQquMfq?p=preview 我已经创建了一个矮人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);
}
}
});
I would pull out the click function into your controller. 我将点击功能插入您的控制器中。 There is no
alert
on your controller's scope so it does nothing. 控制器范围内没有
alert
,因此它什么也不做。 Also, I try to avoid lots of nested quotes if possible. 另外,如果可能,我会尽量避免使用大量嵌套的引号。 A simple and clean refactor.
一个简单干净的重构。
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);
}
}
});
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);
}
}
});
comment 评论
AngularJS doesn't know about alert
. AngularJS不了解
alert
。 If you want to call it from HTML, override it like @udidu showed. 如果要从HTML调用它,请像@udidu所示覆盖它。
When you're using ng-click
the ng-click directive searche for the expression in the current scope. 使用
ng-click
,ng-click指令在当前范围内搜索表达式。 Since their is no alert
function on the directive's scope (or it's parent scope) then nothing happen. 由于它们在指令的作用域(或其父作用域)上没有
alert
功能,因此没有任何反应。
You can add the alert
function 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);
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.