2016-09-25 82 views
0

我建了一個工廠,從數據庫中獲取數據,並傳遞給所有的控制器在我的應用程序是這樣的:爲什麼我的服務不在控制器之間共享數據?

(function() { 
    angular.module('appContacts') 
     .factory('dataService', ['$http', dataService]); 

    function dataService($http) { 

     return { 
      getCurrentOrganization: getCurrentOrganization, 
     }; 

     function getCurrentOrganization(id) { 

      return $http({ 
       method: 'GET', 
       url: '/api/organization/' + id + '/contacts' 
      }) 
     } 
    } 

})(); 

而且我有這樣一個觀點:

<div ng-app="myapp"> 
    <div ng-controller="contactController"> 
    <a ui-sref="organization({Id: organization.id})" ng-click="vm.setCurrentOrganization(organization)"> {{organization.organizationName }}</a> 
</div> 
</div> 

鏈接重定向從查看視圖contactsView.html由第二控制器管理的詳細視圖organizationDetail.html:

.... 
.state("home", { 
     url: "/", 
     templateUrl: "views/contactsView.html", 
     controller: "contactsController", 
     controllerAs: "vm" 
    }) 
    .state("organization", { 
     url: "/organization/:Id", 
     templateUrl: "views/organizationDetail.html", 
     params: { Id: null }, 
     controller: "organizationsController", 
     controllerAs: "vm" 
    }) 
... 

我的問題是,我得到的數據,我見在控制檯中,但是當新的URL到達時,數據不見了,視圖顯示爲空。

如何使用第二個控制器中的工廠生成的數據?

編輯:

這裏是控制器:

//organizationsController.js 
(function() { 
    "use strict"; 
    angular.module('appContacts') 
     .controller('organizationsController', function organizationsController(dataService) { 

      var vm = this; 

      vm.setCurrentOrganization = function (organization) { 
       vm.theOrganization = organization; 
       vm.visible = true; 

    dataService.getCurrentOrganization(vm.theOrganization.id).then(function (result) { 
        vm.organizationData = result.data; 

       }, function() { 
        vm.errorMessage = "Failed to load" + Error; 
       }); 
      } 
    }); 
})(); 

而且contactsController:

//contactsController.js 

(function() { 
    "use strict"; 
    angular.module('appContacts') 
     .controller('contactsController', function contactsController(dataService) { 

      var vm = this; 
      vm.visible = false; 

      activate(); 

      function activate() { 
       dataService.getAllContacts().then(function (result) { 
        vm.allcontacts = result.data; 
       }, function() { 
        vm.errorMessage = "Failed to load" + Error; 
       }); 

       dataService.getAllOrganizations().then(function (result) { 
        vm.organizations = result.data; 
       }, function() { 
        vm.errorMessage = "Failed to load" + Error; 
       }); 
      } 
    }); 
})(); 

的問題是,我點擊視圖中的該llink(contactsView.html/ContactsViewController),我應該在VIEW B(OrganizationDetails.html/organizationController)中使用服務中的數據提取結束。

+0

您是否也可以共享控制器的代碼? – Pradeepb

+0

您是否嘗試過使用$ rootScope或ngStorage? –

+0

@Pradeepb我加了控制器。 –

回答

0

你做錯了這裏

<div ng-app="myapp"> 
    <div ng-controller="contactController"> 
    <a ui-sref="organization({Id: organization.id})" ng-click="vm.setCurrentOrganization(organization)"> {{organization.organizationName }}</a> 
    </div> 
</div> 

contactController不具備的功能setCurrentOrganization。而是在另一個控制器中。您可以從HTML中刪除代碼ng-click="vm.setCurrentOrganization(organization)"。並在organizationsController中使用$stateParams閱讀id。獲得id後,請使用以下服務調用服務:

dataService.getCurrentOrganization(id).then(function (result) { 
     vm.organizationData = result.data; 
    }, function() { 
     vm.errorMessage = "Failed to load" + Error; 
}); 
+0

謝謝,你說的是邏輯。我肯定會試一下 –

+0

。一旦你成功,接受答案。 – Pradeepb

+0

謝謝你!你救了我的命。 接受答案你的意思是按下向上箭頭? –

相關問題