2013-10-31 45 views
0

我有以下两条指令。当我们点击从issuedesc创建按钮ng-click在AngularJS中不起作用

<body> 
<issuedesc></issuedesc> 
</body> 

在浏览器中,上述工程 - 第一个(recentisls)创建第二个 'issuedesc'

directive('recentisls', function ($compile) { 
     return { 
      restrict: 'E', 
      transclude: true, 
      scope: {}, 
      controller: function ($scope, $element) { 
       $scope.showIsDsc = function() {      
        var el = $compile("<issuedesc></issuedesc>")($scope); 
        $('body').html(el); 
      }, 
      template: '<div ng-click="showIsDsc()"></div>', 
     }; 
    }). 

directive('issuedesc', function ($compile) { 
     return { 
      restrict: 'E', 
      transclude: true, 
      scope: {}, 
      controller: function ($scope, $element) { 
       $scope.addcomts = function() { 
        alert("A Hello");      
       }; 
      }, 
      template: '<input ng-click="addcomts()" type="button" value="Submit Comments"/>, 
      replace: true 
     }; 
    }) 

HTML代码。

但是,

<body> 
<recentisls></recentisls> 
</body> 

在浏览器中,上述当我们点击从recentisls创建的按钮不起作用。

回答

0

有一些语法错误

app.directive('recentisls', function ($compile) { 
    return { 
     restrict: 'E', 
     transclude: true, 
     scope: {}, 
     controller: function ($scope, $element) { 
      $scope.showIsDsc = function() { 
       var el = $("<issuedesc></issuedesc>").appendTo('body'); 
       $compile(el)($scope) 
      }; 

     }, 
     template: '<div ng-click="showIsDsc()">dd</div>' 
    } 
}).directive('issuedesc', function ($compile) { 
    return { 
     restrict: 'E', 
     transclude: true, 
     scope: {}, 
     controller: function ($scope, $element) { 
      $scope.addcomts = function() { 
       alert("A Hello"); 
      }; 
     }, 
     template: '<input ng-click="addcomts()" type="button" value="Submit Comments"/>', 
     replace: true 
    }; 
}); 

演示:Fiddle

+0

此代码对我的作品 - VAR EL = $( “”).appendTo ('身体'); $ compile(el)($ scope) – shashankt

0

控制器是不使用$编译和操作DOM的正确位置。这应该在链接功能中完成。在你的情况下,你甚至不需要真正编译。你可以只包括NG-如果到您的模板:

<div ng-if="!issueDescriptionVisible" ng-click="showIsDsc()"></div> 
<issuedesc ng-if="issueDescriptionVisible"></issuedesc> 

那么你的点击处理程序将只是做:

$scope.showIsDsc = function() { 
    $scope.issueDescriptionVisible = true; 
}