採用了棱角分明的引導程序和$模式服務的控制器,我創建了一個彈出與下面的模板:綁定在定父
<div class="dialog modal-header" ng-show="header.length > 0">
<h3 compile="header"></h3>
</div>
<div class="dialog modal-body">
<div compile="bodyTemplate"></div>
</div>
<div class="dialog modal-footer">
<button ng-repeat="button in buttons"
ng-show="$parent.$eval(button.showExpression)"
ng-class="button.class"
ng-click="onButtonClick(button)">
<div compile="button.template"/>
</button>
</div>
我建了一個彈出式服務,幫助在範圍設置默認值,以及作爲從templateUrl載入bodyTemplate。
// snippit of my popup service
function show(modalOptions, scope) {
// ....
// load template from modalOptions.bodyTemplateUrl
// ....
var extendedModalOptions = {};
angular.extend(extendedModalOptions, defaultModelOptions, modalOptions);
extendedModalOptions.controller = function ($scope, $modalInstance) {
angular.extend($scope, scope);
// ....
// add to $scope additional properties from the extendedModalOptions
// such as the buttons array and loaded bodyTemplate
// ....
}
$modal.open(extendedModalOptions);
}
我要的是注入控制器使用的bodyTemplate,就像這樣:
<div class="dialog modal-header" ng-show="header.length > 0">
<h3 compile="header"></h3>
</div>
<!-- Note the addition of ng-controller here -->
<div class="dialog modal-body" ng-controller="bodyController">
<div compile="bodyTemplate"></div>
</div>
<div class="dialog modal-footer">
<button ng-repeat="button in buttons"
ng-show="$parent.$eval(button.showExpression)"
ng-class="button.class"
ng-click="onButtonClick(button)">
<div compile="button.template"/>
</button>
</div>
這可以工作,但是,bodyController範圍的$父現在是給範圍$模式控制器。我想使用不同的父母。
我想能夠用我的服務彈出這樣的:
// from inside CustomerController
popup.show({
header: 'Test Popup',
parentScope: $scope, // the current scope of CustomerController
bodyTemplateUrl: 'testPopupTemplate.html',
bodyController: 'TestPopupController as testPopupVM'
});
這是,我有點失落。我想我的彈出式服務可以創建這樣的控制器:
$scope.bodyController = $controller(extendedModalOptions.bodyController, {
$scope: extendedModalOptions.parentScope
});
我不是100%肯定,但我認爲,讓我用正確的父控制器。但問題是,返回的值是新的作用域對象,而不是構造函數。
根據ng-controller上的angular docs,它只能綁定到構造函數。
如何在使用提供的父範圍時將主體控制器添加到彈出模板?