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

从数据库编译动态HTML字符串

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

从数据库编译动态HTML字符串

ng-bind-html-unsafe
仅将内容呈现为HTML。它不会将Angular范围绑定到结果DOM。您必须
$compile
为此目的使用服务。我创建了这个插件,以演示如何用于
$compile
创建指令来呈现用户输入的动态HTML并将其绑定到控制器的作用域。来源发布在下面。

demo.html

<!DOCTYPE html><html ng-app="app">  <head>    <script data-require="angular.js@1.0.7" data-semver="1.0.7" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.js"></script>    <script src="script.js"></script>  </head>  <body>    <h1>Compile dynamic HTML</h1>    <div ng-controller="MyController">      <textarea ng-model="html"></textarea>      <div dynamic="html"></div>    </div>  </body></html>

script.js

var app = angular.module('app', []);app.directive('dynamic', function ($compile) {  return {    restrict: 'A',    replace: true,    link: function (scope, ele, attrs) {      scope.$watch(attrs.dynamic, function(html) {        ele.html(html);        $compile(ele.contents())(scope);      });    }  };});function MyController($scope) {  $scope.click = function(arg) {    alert('Clicked ' + arg);  }  $scope.html = '<a ng-click="click(1)" href="#">Click me</a>';}


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

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

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