我正在尝试动态编译一个指令。此指令具有隔离作用域。这样的东西:
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
。如何将范围传递到指令中?
以下是如何使用链接将范围传递到指令中。
link: function (scope, element, attrs) {
scope.text = "hello";
}