首页 > 代码库 > service $sce or ng-bind-html
service $sce or ng-bind-html
ng-bind-html:
一般搭配 $sce.trutsAsResourceUrl $sce.trutsAsHtml
<html> <head> <meta charset="utf-8"> <script src="js/angular.js"></script> </head> <body> <div ng-app="myApp" ng-controller="myCtrl"> 未处理的: <div ng-repeat="item in formData"> {{item.name}} :{{item.htmlVal}} </div> <br/>处理过的:<button ng-click="look()">查看处理结果</button> <div ng-repeat="item in data"> {{item.name}} :<p ng-bind-html="item.htmlVal"></p> </div> </div> <script> var app = angular.module(‘myApp‘, []); app.controller(‘myCtrl‘, function($scope,$sce){ //未处理数据源 $scope.formData=[ {"name":"大鸟","htmlVal":"我是<span style=‘color:red;‘>大鸟<span>"}, {"name":"小鸟","htmlVal":"我是<span style=‘color:red;‘>小鸟<span>"} ]; //处理结果 $scope.look = function(){ $scope.data=[ {"name":"大鸟","htmlVal":"我是<span style=‘color:red;‘>大鸟<span>"}, {"name":"小鸟","htmlVal":"我是<span style=‘color:red;‘>小鸟<span>"} for(var i=0;i<$scope.data.length;i++){ $scope.data[i].htmlVal = $sce.trustAsHtml($scope.data[i].htmlVal); } }; }); </script> </body> </html>
service $sce or ng-bind-html
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。