Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 添加带有ng repeat的html,其中html还包含ng repeat_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 添加带有ng repeat的html,其中html还包含ng repeat

Javascript 添加带有ng repeat的html,其中html还包含ng repeat,javascript,html,angularjs,Javascript,Html,Angularjs,我有这样的代码,它使用ng repeat将html代码绑定到div <div ng-repeat="item in items" ng-bind-html="item.html"> 有什么建议可以让它工作吗?您必须首先使用$compile服务编译html。你可以看看这些文件 $compile您必须首先使用$compile服务编译html。你可以看看这些文件 $compileng bind html不会为您编译内容 您可以改为使用此指令,它基于ng bind html的源代码

我有这样的代码,它使用ng repeat将html代码绑定到div

<div ng-repeat="item in items" ng-bind-html="item.html">    

有什么建议可以让它工作吗?

您必须首先使用
$compile
服务编译html。你可以看看这些文件


$compile

您必须首先使用
$compile
服务编译html。你可以看看这些文件


$compile

ng bind html
不会为您编译内容

您可以改为使用此指令,它基于
ng bind html
的源代码,但经过修改后也可以编译:

app.directive('compileHtml', ['$sce', '$parse', '$compile',
  function($sce, $parse, $compile) {
    return {
      restrict: 'A',
      compile: function ngBindHtmlCompile(tElement, tAttrs) {
        var ngBindHtmlGetter = $parse(tAttrs.compileHtml);
        var ngBindHtmlWatch = $parse(tAttrs.compileHtml, function getStringValue(value) {
          return (value || '').toString();
        });
        $compile.$$addBindingClass(tElement);

        return function ngBindHtmlLink(scope, element, attr) {
          $compile.$$addBindingInfo(element, attr.compileHtml);

          scope.$watch(ngBindHtmlWatch, function ngBindHtmlWatchAction() {

            element.html($sce.trustAsHtml(ngBindHtmlGetter(scope)) || '');
            $compile(element.contents())(scope);
          });
        };
      }
    };
  }
]);
用法:

<div ng-repeat="item in items" compile-html="item.html">   

ng bind html
不会为您编译内容

您可以改为使用此指令,它基于
ng bind html
的源代码,但经过修改后也可以编译:

app.directive('compileHtml', ['$sce', '$parse', '$compile',
  function($sce, $parse, $compile) {
    return {
      restrict: 'A',
      compile: function ngBindHtmlCompile(tElement, tAttrs) {
        var ngBindHtmlGetter = $parse(tAttrs.compileHtml);
        var ngBindHtmlWatch = $parse(tAttrs.compileHtml, function getStringValue(value) {
          return (value || '').toString();
        });
        $compile.$$addBindingClass(tElement);

        return function ngBindHtmlLink(scope, element, attr) {
          $compile.$$addBindingInfo(element, attr.compileHtml);

          scope.$watch(ngBindHtmlWatch, function ngBindHtmlWatchAction() {

            element.html($sce.trustAsHtml(ngBindHtmlGetter(scope)) || '');
            $compile(element.contents())(scope);
          });
        };
      }
    };
  }
]);
用法:

<div ng-repeat="item in items" compile-html="item.html">   


它应该像您编写的那样工作。你确定你的angular应用程序引导正确吗?你有任何控制台错误吗?它应该像你写的那样工作。你确定你的angular应用程序引导正确吗?你有任何控制台错误吗?