更新是因为对我的要求有些困惑。我想使用指令将变量注入(inject)该指令使用的 Controller 。我知道我可以为此使用 $scope,但我认为这不是一个直观的解决方案。
本质上,我希望我的 Controller 将提案变量注入(inject)其中。
我的预期用途:
<blah-directive proposal="proposal"></blah-directive>
指令(到目前为止):
app.directive('blahDirective', function () {
return {
restrict: 'E'
, transclude: true
, replace: true
, scope: {
proposal: '='
}
, templateUrl: 'blahTemp.html'
, controller: blahController
};
});
blahTemp.html
<form class="form-horizontal" role="form" name="myBidForm">
**{{ proposal }}**
</form>
这很好地在 $scope 中显示了值建议变量,但这不是我想要的。本质上,我想像这样定义我的 Controller :
var blahController = function($scope, SomeOtherResource, proposal) {
}
最佳答案
如果你想将局部变量注入(inject) Controller 使用$controller .
这是一个例子(plunker):
app.directive('blahDirective', function ($controller) {
return {
restrict: 'E',
scope: {
proposal : "="
},
transclude: true,
replace: true,
templateUrl: 'blahTemp.html',
link : function (scope, elm, attrs){
scope.proposal = {};
var locals = {
$scope: scope ,
proposal: scope.proposal
};
$controller('blahController', locals);
}
};
});
关于javascript - 从 Angular 指令将变量注入(inject) Angular Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21172689/