我怎样才能将隔离范围注入指令?

时间:2016-03-31 20:50:00

标签: angularjs

我试图动态编译指令。该指令具有隔离范围。所以像这样:

angular.module('mod').directive 'foo', ->
    restrict: 'E'
    templateUrl: 'foo.html'
    scope:
        text: '=text'
    bindToController: true
    replace: true
    controllerAs: 'fooCtrl'
    controller: ($scope) ->
        console.log @ # .text undefined
        console.log $scope # .text undefined
        return

以下是我的编译方式:

template = "<foo></foo>"
scope = $rootScope.$new()
scope.text = "hello"
$compile(template) scope, (clone, innerScope) -> 
    angular.element('body').append clone

但是,在记录@时,文字为undefined。如何将范围传递给我的指令?

1 个答案:

答案 0 :(得分:0)

以下是使用link将范围传递给指令的方法。

link: function (scope, element, attrs) {
  scope.text = "hello";
}