0
我有一個帶有確認和取消方法的父指令模式指令。如何從子指令中調用AngularJS父指令作用域方法
ngDialog.open({
scope : true,
showClose : false,
template : theTemplate,
className : 'ngdialog-theme-prompt -small',
controller : ['$scope', function Ctrl($scope) {
$scope.message = message;
$scope.confirm = function(){
$scope.closeThisDialog();
resolve(true);
};
$scope.cancel = function(){
$scope.closeThisDialog();
resolve(false);
};
}]
}).closePromise.then(function() {
resolve(false);
})
模板是另一個指令。
<form class="edit-stakes-content">
<ul>
<li ng-repeat="stake in settings track by $index">
<input type="number" name="input" pattern="^\d+$" min="0" value="{{stake}}" />
</li>
</ul>
<div class="edit-stakes-control">
<button ng-click="cancel();" class="apl-btn apl-btn-link apl-btn-large" translate>CANCEL</button>
<button ng-click="confirm();" autofocus="true" class="apl-btn apl-btn-update apl-btn-large" translate>SAVE</button>
</div>
</form>
在我希望能夠進行一些驗證和POST到服務器,然後調用父模態指令,確認或取消方法,模態的子指令。
與此類似:
Directive.$inject = ['UserSettings'];
function Directive(UserSettings) {
function Link($scope) {
$scope.settings = angular.copy(UserSettings.get('presetStakeSettings').settings);
$scope.cancel = $scope.$parent.cancel();
}
return {
'link': Link,
'restrict': 'E',
'replace': true,
'templateUrl': 'betting/edit-stakes/edit-stakes.html'
}
}
調用父類的方法拋出一個JavaScript錯誤出現在$範圍$ parent.cancel不是函數又似乎做的工作。
有沒有更好的方式來實現孩子到家長溝通沒有JavaScript錯誤?