首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ng-在将已编译html设置为div后不起作用的指令。

ng-在将已编译html设置为div后不起作用的指令。
EN

Stack Overflow用户
提问于 2018-04-28 01:40:19
回答 1查看 44关注 0票数 0

在将编译后的html设置为dynamic-html div之前,我的dynamicHtml工作正常。如您所见,指令中有一个onClick函数。

但不幸的是,在将编译后的html设置为div后,不再调用onClick

代码语言:javascript
复制
var content = $compile(res)($scope);
$('dynamic-html').html(content);

我的dynamicHTML指令如下所示:

代码语言:javascript
复制
.directive('dynamicHtml', function($compile, $timeout) {
  return {
    restrict: 'E',
    transclude: true,
    link: function($scope, $element) {
      $scope.datePickers = {};

      $scope.onClick = function (variable, $event) {
        var currentTarget = $event.currentTarget;

          $scope.$parent.$parent.highlight(variable, true, currentTarget);
      };

如您所见,我尝试在指令中添加transclude。但这案子不管用。你能指导我解决这个问题吗?

EN

回答 1

Stack Overflow用户

发布于 2018-04-28 08:49:37

看看下面的代码,

代码语言:javascript
复制
angular.module("myApp", [])

  .directive("compiled", function($compile){
    return {
      restrict: "AE",
      scope: {},
      link: function(scope, ele, attrs){
          var compiled = $compile("<div><hello-world></hello-world></div>")(scope);
          ele.replaceWith(compiled);
      }
    }
  })

  .directive("helloWorld", function(){
    return {
      restrict: "AE",
      scope: {},
      transculde: true,
      link: function(scope, ele, attrs){
        scope.sayhello = function(){
          alert("Hello World");
        }
      },
      template: '<button ng-click="sayhello()">Say Hello</button>'
    }
  })

代码语言:javascript
复制
<compiled></compiled>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50072031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档