【发布时间】:2016-12-13 22:50:47
【问题描述】:
我在我的应用中遇到了多次触发共享数据服务的问题,现在我将尝试解释它。
在带有控制器“主页”的主页上,用户可以选择三个菜单项之一,在用户选择其中一项(例如第一个)后,他将重定向到此视图,并带有自己的控制器“表” '。该视图的底部有一个表格和选项卡,每个选项卡都有自己的控制器“tab1 等”。通过单击行,我在选项卡的控制器之间共享该行的 ID,该选项卡可以加载与该特定行相关的内容。目前一切正常。
接下来,当用户点击主页按钮时,他将重定向到主页,如果他再次点击项目(例如第一个),共享服务将触发两次并返回相同的 id 两次,第三次分享服务会触发3次等等。
在我的代码下方,如果有人可以解释我所缺少的内容,我将不胜感激。提前致谢。
app.controller('table', [
'$scope', 'tab1ID', 'tab2ID', 'tab3ID'
function ($scope, tab1ID, tab2ID, tab3ID) {
$scope.rowSelection($scope, function (row) {
tab1ID.setId(row.id);
tab2ID.setId(row.id);
tab3ID.setId(row.id)
});
app.controller('tab1', [
'$scope', 'tab1ID', dataService
function ($scope, tab1ID, dataService) {
tab1ID.getId().then(null, null, function (id) {
// return increment count of 'id' after redirecting from home view to table view
// dataService firing for each incremented id (1 for 1, 2 for 2 and etc)
dataService.rowData(id)
.then(function (res) {
$scope.data = res;
})
});
.factory(
'tab1ID', function ($q) {
var self = this,
defer = $q.defer();
this.share = '';
return {
getId: function () {
return defer.promise;
},
setId: function (id) {
self.share = id;
defer.notify(self.share);
}
}
}
);
【问题讨论】:
标签: javascript angularjs angular-services