猿问

AngularJS 能不能不用ng-bind-html绑定显示HTML?

AngularJS 能不能不用ng-bind-html绑定显示HTML


交互式爱情
浏览 777回答 1
1回答

Cats萌萌

用{{}}是一定没法输出html的不知道你项目里是怎么限制的不能使用 ng-bind-html那你试试自己写个指令来实现一下好了12345678910111213var&nbsp;app&nbsp;=&nbsp;angular.model('app'&nbsp;,&nbsp;[]);app.directive('stringHtml'&nbsp;,&nbsp;function(){&nbsp;&nbsp;return&nbsp;function(scope&nbsp;,&nbsp;el&nbsp;,&nbsp;attr){&nbsp;&nbsp;&nbsp;&nbsp;if(attr.stringHtml){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;scope.$watch(attr.stringHtml&nbsp;,&nbsp;function(html){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;el.html(html&nbsp;||&nbsp;'');//更新html内容&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;});&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;&nbsp;};});app.controller('TestCtrl'&nbsp;,&nbsp;function($scope){&nbsp;&nbsp;$scope.string&nbsp;=&nbsp;'<button&nbsp;type="button">测试html按钮</button>';});html:123<div&nbsp;ng-controller="TestCtrl">&nbsp;&nbsp;<span&nbsp;string-html="string"></span></div>
随时随地看视频慕课网APP

相关分类

AngularJS
我要回答