ng-click not working inside the controller using of ng-bind-html

有些话、适合烂在心里 提交于 2019-12-05 16:16:43

The reason behind you ng-click not working is because, ng-bind-html doens't compile div, You should use ng-if there OR compile 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

This code will solve your issue. add this directive in your controller.

directive('compileTemplate', function($compile, $parse){
        return {
            link: function(scope, element, attr){
                var parsed = $parse(attr.ngBindHtml);
                function getStringValue() { return (parsed(scope) || '').toString(); }

                //Recompile if the template changes
                scope.$watch(getStringValue, function() {
                    $compile(element, null, -9999)(scope);  //The -9999 makes it skip directives so that we do not recompile ourselves
                });
            }
        }
    })

and your HTML will be like this:

<div id ="section1" ng-bind-html="divHtmlVariable" compile-template></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 compile the html you included

.directive('compile', ['$compile', function ($compile) {
  return function(scope, element, attrs) {
    scope.$watch(
        function(scope) {
            return scope.$eval(attrs.compile);
        },
        function(value) {
            element.html(value);
            $compile(element.contents())(scope);
        }
    );
  };
}])

Then you can reference firstName as <div compile="firstName"><div>

Try this ... this link solve my problem code :Link code
Add directive

 myApp.directive('compile', ['$compile', function ($compile) 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!