码迷,mamicode.com
首页 > Web开发 > 详细

AngularJS $compile动态生成html

时间:2016-04-22 16:32:21      阅读:903      评论:0      收藏:0      [点我收藏+]

标签:

angular.module(‘app‘)
    .directive(‘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);
              }
            );
        };
    });

  

   <div ng-app="app" ng-controller="SampleCtrl" class="container">
        <div ng-repeat="item in items">
            {{item.name}}<br />
            <div compile="item.tempatle"></div>          
        </div>    
    </div>

  

 <script>
        angular
            .module(‘app‘)
            .controller(‘SampleCtrl‘, function ($scope, $filter) {

                $scope.open = function () {
                    alert("ddd");
                }
                $scope.items = [
                    { "name": "zhangsan", tempatle: ‘<i class="searchicon fa fa-search"   ng-click="open()" style="color:#5db2ff;cursor:pointer;">点击</i>‘ },
                    { "name": "lisi", tempatle: ‘<i class="searchicon fa fa-search"   ng-click="open()" style="color:#5db2ff;cursor:pointer;">点击</i>‘ },
                    { "name": "wangwu", tempatle: ‘<i class="searchicon fa fa-search"   ng-click="open()" style="color:#5db2ff;cursor:pointer;">点击</i>‘ }]

            });
    </script>

  

AngularJS $compile动态生成html

标签:

原文地址:http://www.cnblogs.com/lkd3063601/p/5421364.html

(0)
(1)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!