Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.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 Angularjs-带有ng bind html的内联指令不安全_Javascript_Angularjs - Fatal编程技术网

Javascript Angularjs-带有ng bind html的内联指令不安全

Javascript Angularjs-带有ng bind html的内联指令不安全,javascript,angularjs,Javascript,Angularjs,我的目标是从一段文本中提取所有的hashtag,并用一个指令替换它们,这意味着它应该这样做: <p>Hello #stackoverflow this is a #test<p> 你好#stackoverflow这是一个#测试 进入 你好,这是一个测试 我的想法是使用一个过滤器来用指令html替换hashtag,但我不知道如何显示它,因为ng bind html unsafe显然没有编译指令 有任何提示吗?创建一个新指令,将HTML字符串附加到DOM后编译为模板:

我的目标是从一段文本中提取所有的hashtag,并用一个指令替换它们,这意味着它应该这样做:

<p>Hello #stackoverflow this is a #test<p>
你好#stackoverflow这是一个#测试
进入

你好,这是一个测试
我的想法是使用一个过滤器来用指令html替换hashtag,但我不知道如何显示它,因为ng bind html unsafe显然没有编译指令


有任何提示吗?

创建一个新指令,将HTML字符串附加到DOM后编译为模板:

angular.module('myCompile', [], ['$compileProvider', function($compileProvider) {
  // Allows an attribute's value to be evaluated and compiled against the scope, resulting
  // in an angularized template being injected in its place.
  //
  // Note: This directive is suffixed with "unsafe" because it does not sanitize the HTML. It is up
  // to the developer to ensure that the HTML is safe to insert into the DOM.
  //
  // Usage:
  //     HTML: <div my-compile-unsafe="templateHtml"></div>
  //     JS: $scope.templateHtml = '<a ng-onclick="doSomething()">Click me!</a>';
  //     Result: DIV will contain an anchor that will call $scope.doSomething() when clicked.
  $compileProvider.directive('myCompileUnsafe', ['$compile', function($compile) {
    return function(scope, element, attrs) {
      scope.$watch(
        function(scope) {
          // watch the 'compile' expression for changes
          return scope.$eval(attrs.myCompileUnsafe);
        },
        function(value) {
          // when the 'compile' expression changes
          // assign it into the current DOM element
          element.html(value);

          // compile the new DOM and link it to the current
          // scope.
          // NOTE: we only compile .childNodes so that
          // we don't get into infinite loop compiling ourselves
          $compile(element.contents())(scope);
        }
      );
    };
  }]);
}]);
angular.module('myCompile',[],['$compileProvider',函数($compileProvider){
//允许根据作用域计算和编译属性值,从而
//在一个有角度的模板中被注入到它的位置。
//
//注意:此指令的后缀为“不安全”,因为它没有清理HTML。它是启动的
//向开发人员报告,以确保HTML可以安全地插入到DOM中。
//
//用法:
//HTML:
//JS:$scope.templateHtml='Click me!';
//结果:DIV将包含一个锚点,单击该锚点时将调用$scope.doSomething()。
$compileProvider.directive('myCompileUnsafe',['$compile',函数($compile){
返回函数(范围、元素、属性){
范围.$watch(
功能(范围){
//观察“compile”表达式的变化
返回范围:$eval(属性myCompileUnsafe);
},
函数(值){
//当“compile”表达式更改时
//将其分配到当前DOM元素中
html(值);
//编译新DOM并将其链接到当前
//范围。
//注意:我们只编译.childNodes,以便
//我们自己并没有陷入无限循环
$compile(element.contents())(范围);
}
);
};
}]);
}]);

创建一个新指令,将HTML字符串附加到DOM后编译为模板:

angular.module('myCompile', [], ['$compileProvider', function($compileProvider) {
  // Allows an attribute's value to be evaluated and compiled against the scope, resulting
  // in an angularized template being injected in its place.
  //
  // Note: This directive is suffixed with "unsafe" because it does not sanitize the HTML. It is up
  // to the developer to ensure that the HTML is safe to insert into the DOM.
  //
  // Usage:
  //     HTML: <div my-compile-unsafe="templateHtml"></div>
  //     JS: $scope.templateHtml = '<a ng-onclick="doSomething()">Click me!</a>';
  //     Result: DIV will contain an anchor that will call $scope.doSomething() when clicked.
  $compileProvider.directive('myCompileUnsafe', ['$compile', function($compile) {
    return function(scope, element, attrs) {
      scope.$watch(
        function(scope) {
          // watch the 'compile' expression for changes
          return scope.$eval(attrs.myCompileUnsafe);
        },
        function(value) {
          // when the 'compile' expression changes
          // assign it into the current DOM element
          element.html(value);

          // compile the new DOM and link it to the current
          // scope.
          // NOTE: we only compile .childNodes so that
          // we don't get into infinite loop compiling ourselves
          $compile(element.contents())(scope);
        }
      );
    };
  }]);
}]);
angular.module('myCompile',[],['$compileProvider',函数($compileProvider){
//允许根据作用域计算和编译属性值,从而
//在一个有角度的模板中被注入到它的位置。
//
//注意:此指令的后缀为“不安全”,因为它没有清理HTML。它是启动的
//向开发人员报告,以确保HTML可以安全地插入到DOM中。
//
//用法:
//HTML:
//JS:$scope.templateHtml='Click me!';
//结果:DIV将包含一个锚点,单击该锚点时将调用$scope.doSomething()。
$compileProvider.directive('myCompileUnsafe',['$compile',函数($compile){
返回函数(范围、元素、属性){
范围.$watch(
职能(范围){
//观察“compile”表达式的变化
返回范围:$eval(属性myCompileUnsafe);
},
函数(值){
//当“compile”表达式更改时
//将其分配到当前DOM元素中
html(值);
//编译新DOM并将其链接到当前
//范围。
//注意:我们只编译.childNodes,以便
//我们自己并没有陷入无限循环
$compile(element.contents())(范围);
}
);
};
}]);
}]);