2013-10-29 55 views
0

我是Angular JS的新手。我有以下的角度指令如何使用jquery動態添加角度指令

angular.module('components', []). directive('newissueedtr', function() { return { restrict: 'E', transclude: true, scope: {}, template: '<div><textarea></textarea></div>', replace: true };

我想從jQuery的,像動態添加此指令,

$('body').append('<newissueedtr></newissueedtr>') 

以上jQuery代碼不工作。可能嗎?或者還有其他方法。

+0

,如果這是一個角碼之內發生了什麼?你需要掌握'$ compile' –

回答

0

這裏是不相同的u需要修改,按您的需求的例子.. http://jsfiddle.net/paulocoelho/fBjbP/2/

var module = angular.module('testApp', []) 
    .directive('test', function ($compile) { 
    return { 
     restrict: 'E', 
     scope: { 
      text: '@' 
     }, 
     template: '<p ng-click="add()">{{text}}</p>', 
     controller: function ($scope, $element) { 
      $scope.add = function() { 
       var el = $compile("<test text='n'></test>")($scope); 
       $element.parent().append(el); 
      }; 
     } 
    }; 
}); 

function crtl($scope) {}