Angular: ng-bind-html filters out ng-click?

前端 未结 3 1553
暖寄归人
暖寄归人 2020-11-29 04:55

I have some html data that I\'m loading in from a json file.

I am displaying this html data by using ngSanitize in my app and using ng-bind-html.

Now I woul

3条回答
  •  心在旅途
    2020-11-29 05:04

    Ok, so the issue is that it isn't compiling the html you include (angular isn't parsing it to find directives and whatnot). Can't think of a way to make it to compile from within the controller, but you could create a directive that includes the content, and compiles it.

    So you would change

    to

    And then for the js:

    var myApp = angular.module('myApp', ['ngSanitize']);
    angular.module('myApp')
    .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);
          }
       )};
      }]).controller('MyCtrl', function($scope) {
        var str = 'hello http://www.cnn.com';
        var urlRegEx = /((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:\/[\+~%\/\.\w\-]*)?\??(?:[\-\+=&;%@\.\w]*)#?(?:[\.\!\/\\\w]*))?)/g;
        result = str.replace(urlRegEx, "$1");
        $scope.GotoLink = function() { alert(); }
        $scope.name = result;
    });
    

    Angular 1.2.12: http://jsfiddle.net/7k8xJ/4/

    Angular 1.4.3: http://jsfiddle.net/5g6z58yy/ (same code as before, but some people were saying it doesn't work on 1.4.*)

提交回复
热议问题