admin管理员组文章数量:1290991
below my code ng-click not working,while i am checking the inspect element ng-click not showing, help me how to do
var app = angular.module('myApp', ['ngSanitize']);
app.controller('myCtrl', function($scope) {
$scope.firstName = "<b ng-click=test(1)>John</b><br><b ng-click=test1(1)>Testing</b>";
$scope.test=function(val)
{
alert(val)
}
$scope.test1=function(val)
{
alert(val)
}
});
<!DOCTYPE html>
<html>
<script src= ".0.3/angular.min.js"></script>
<script src= ".0.3/angular-sanitize.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<span ng-bind-html=firstName><span>
</div>
</body>
</html>
below my code ng-click not working,while i am checking the inspect element ng-click not showing, help me how to do
var app = angular.module('myApp', ['ngSanitize']);
app.controller('myCtrl', function($scope) {
$scope.firstName = "<b ng-click=test(1)>John</b><br><b ng-click=test1(1)>Testing</b>";
$scope.test=function(val)
{
alert(val)
}
$scope.test1=function(val)
{
alert(val)
}
});
<!DOCTYPE html>
<html>
<script src= "http://ajax.googleapis./ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script src= "http://ajax.googleapis./ajax/libs/angularjs/1.0.3/angular-sanitize.js"></script>
<body>
<div ng-app="myApp" ng-controller="myCtrl">
<span ng-bind-html=firstName><span>
</div>
</body>
</html>
Share
Improve this question
edited Aug 5, 2015 at 7:15
Pankaj Parkar
136k23 gold badges240 silver badges303 bronze badges
asked Aug 5, 2015 at 6:55
user4277191user4277191
4 Answers
Reset to default 4This code will solve your issue. add this directive in your controller.
directive('pileTemplate', function($pile, $parse){
return {
link: function(scope, element, attr){
var parsed = $parse(attr.ngBindHtml);
function getStringValue() { return (parsed(scope) || '').toString(); }
//Repile if the template changes
scope.$watch(getStringValue, function() {
$pile(element, null, -9999)(scope); //The -9999 makes it skip directives so that we do not repile ourselves
});
}
}
})
and your HTML will be like this:
<div id ="section1" ng-bind-html="divHtmlVariable" pile-template></div>
The reason behind you ng-click
not working is because, ng-bind-html
doens't pile div, You should use ng-if
there OR pile a div and add that element from directive instead of controller.
Markup
<div ng-app="myApp" ng-controller="myCtrl">
<span ng-if=showFirstName>
<b ng-click=test(1)>John</b><br><b ng-click=test1(1)>Testing</b>
<span>
</div>
Code
$scope.showFirstName = true;//for showing div
The issue is Angular won't parse the directives inside your ng-bind-html
.
A proper solution to this is creating a directive yourself to pile the html you included
.directive('pile', ['$pile', function ($pile) {
return function(scope, element, attrs) {
scope.$watch(
function(scope) {
return scope.$eval(attrs.pile);
},
function(value) {
element.html(value);
$pile(element.contents())(scope);
}
);
};
}])
Then you can reference firstName
as <div pile="firstName"><div>
Try this ... this link solve my problem
code :Link code
Add directive
myApp.directive('pile', ['$pile', function ($pile)
本文标签: javascriptngclick not working inside the controller using of ngbindhtmlStack Overflow
版权声明:本文标题:javascript - ng-click not working inside the controller using of ng-bind-html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741516664a2382931.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论