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
Add a ment  | 

4 Answers 4

Reset to default 4

This 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