admin管理员组文章数量:1336660
I have code similar to the below code to trigger a click
event in an Angular app. Why is not the event triggering?
var app = angular.module("myApp", [])
app.directive('myTop',function($pile) {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function (scope, element) {
var childElement = '<button ng-click="clickFunc()">CLICK</button>';
element.append(childElement);
$pile(childElement)(scope);
scope.clickFunc = function () {
alert('Hello, world!');
};
}
}
})
I have code similar to the below code to trigger a click
event in an Angular app. Why is not the event triggering?
var app = angular.module("myApp", [])
app.directive('myTop',function($pile) {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function (scope, element) {
var childElement = '<button ng-click="clickFunc()">CLICK</button>';
element.append(childElement);
$pile(childElement)(scope);
scope.clickFunc = function () {
alert('Hello, world!');
};
}
}
})
Share
Improve this question
edited Dec 10, 2015 at 9:26
Shashank Agrawal
25.8k11 gold badges96 silver badges125 bronze badges
asked Dec 10, 2015 at 2:28
freddy83freddy83
631 silver badge5 bronze badges
1 Answer
Reset to default 7Change your pile statement like this:
$pile(element.contents())(scope);
You were passing a DOM string childElement
which is really not a DOM element instead it is a string. But the $pile
needs the DOM element(s) to actually pile the content.
var app = angular.module("myapp", []);
app.directive('myTop', ['$pile',
function($pile) {
return {
restrict: 'E',
template: '<div></div>',
replace: true,
link: function(scope, element) {
var childElement = '<button ng-click="clickFunc()">CLICK</button>';
element.append(childElement);
$pile(element.contents())(scope);
scope.clickFunc = function() {
alert('Hello, world!');
};
}
}
}
])
<html>
<body ng-app="myapp">
<script src="https://ajax.googleapis./ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<my-top></my-top>
</body>
</html>
本文标签: javascriptAngular ngclick not working with compileStack Overflow
版权声明:本文标题:javascript - Angular ng-click not working with $compile - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742217826a2434891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论