我在阅读@toddmotto优秀的post,this,this和this答案,api之后来到现在我现在有了ctrl结构像这样
<div ng-controller="ParentCtrl as parent" class="ng-scope">
<div ng-controller="SiblingOneCtrl as sib1" class="ng-scope"></div>
<div ng-controller="SiblingTwoCtrl as sib2" class="ng-scope"></div>
<div class="btn btn-primary"
ng-click="parent.events.saveDreaft()">
<span class="glyphicon glyphicon-ok"></span>
<span>Save</span>
</div>
</div>
&安培;&安培;
define(['app'], function (app) {
app.controller("ParentCtrl", ['$scope', '$q', function ($scope, $q) {
var parent= this;
parent.data = {};
parent.events = {
saveDreaft: function() {
$scope.$broadcast('saveDreaft');
//return $q(function(resolve, reject) {
// try {
// var ee = $scope.$broadcast('saveDreaft');
// if (ee)
// resolve();
// } catch (ex) {
// reject(ex);
// }
//});
}
};
}]);
});
//=======
define(['app'], function (app) {
app.controller("SiblingOneCtrl", ['$scope', '$http', '$q',
function ($scope, $http, $q) {
var sib1 = this;
sib1.events = {
saveSib1Form: function () {
return $http.post('..', ..)
.then(function (res) {..})
.catch(function (ex) {..});
}
};
var unbind = $scope.$on('saveDreaft', function (event, data) {
sib1.events.saveSib1Form();
//return $q.when(sib1.events.saveSib1Form)
// .then(function(res) {
// event.targetScope.callbackRes = {
// res: res,
// flag: true
// };
// })
// .catch(..);
});
$scope.$on('$destroy', unbind);
}]);
});
当客户端从父作用域单击保存时,我们希望子进程运行自己的保存方法,然后向发布者发送一些回调,例如jQuery.Callbacks()
他完成的$q
承诺。
有什么建议吗?
答案 0 :(得分:2)
您可以使用$emit
在子范围内实现类似回调的功能:
sib1.events.saveSib1Form();
$scope.$emit('saveComplete', args);
在父范围内注册$emit
事件:
$scope.$on('saveComplete', function(event, args) {
//your callback code
})