【发布时间】:2013-10-31 23:59:56
【问题描述】:
我正在尝试通过指令将模板传递给动态控制器(从指令的角度来看,在运行时已知)。
类似这样的:
<my-dir ctrl="PersonCtrl">
<h1>{{person.name}} - {{person.age}}</h1>
</my-dir>
var data = {
name: "Alex",
age: "24"
};
function PersonCtrl($scope){
$scope.person = data;
}
myApp.directive('myDir', function($controller){
return {
restrict: "EA",
scope: {
ctrl: "@"
},
transclude: true,
controller: function($scope, $element, $attrs) {
},
template: "<div>{{ctrl}}</div><div ng-transclude></div>",
link: function($scope, $element, $attrs) {
$controller($attrs.foo, {$scope: {}});
}
};
});
找到并实例化了控制器,但不知何故将嵌入的模板绑定到它不起作用。我错过了一些订单要求还是有办法将此控制器范围绑定到嵌入模板?
【问题讨论】:
标签: angularjs angularjs-directive angularjs-scope angularjs-controller transclusion