栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

角度:ng-bind-html过滤掉ng-click?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

角度:ng-bind-html过滤掉ng-click?

好的,所以问题在于它不会编译您包含的html(Angular不会解析它以查找指令等)。想不出一种方法可以从控制器内部进行编译,但是您可以创建一个包含内容的指令,然后对其进行编译。

所以你会改变

<p ng-bind-html="name"></p>

<p compile="name"></p>

然后对于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, "<a ng-click="Gotolink('$1','_system')">$1</a>");    $scope.Gotolink = function() { alert(); }    $scope.name = result;});

Angular1.2.12:http://jsfiddle.net/7k8xJ/4/

Angular1.4.3:http://jsfiddle.net/5g6z58yy/(与以前的代码相同,但有人说它在1.4上不起作用。*)



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/633205.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号