2016-08-18 89 views
0

我想運行一個服務,當用戶訪問幾個特定頁面時,該服務會讀取總未讀消息。我爲此使用了resolve。我建立了一個通過http調用與後端進行通信的工廠,後端返回了總消息的數量,我想在html頁面中顯示,但是我得到的所有錯誤都是錯誤的。

(function() { 

    var countAllUnreads = function($location, $q, AuthService3) 
    { 
     var deferred = $q.defer(); 
     AuthService3.fetchUnreadNotifications().then(function (res) 
     { 
     console.log('this is me'); 
     $scope.numOfNotifications =res.data.totalUnread; 
     }); 
    } 
    angular.module('myApp', [ 
     'ngRoute', 
     'myApp.login', 
     'myApp.home', 
     'myApp.directory', 
     'myApp.forgotpassword', 
     'myApp.myProfile', 
    ]) 

    .factory('AuthService3', ["$http", "$location", function($http, $location){ 
     var baseUrl = 'api/'; 
     var fetchUnreadNotifications = function() 
     { 

     return $http.post(baseUrl + 'getAllUnreadNotifications');   
     }  

     return {fetchUnreadNotifications: fetchUnreadNotifications} ; 
    }]) 
    .config(['$routeProvider', function($routeProvider) { 
     $routeProvider 
     .when('/login', { 
      controller: 'LoginController', 
      templateUrl: 'app/components/login/loginView.html',  
      controllerAs: 'vm' 
     }) 
     .when('/forgotpassword', { 
      controller: 'ForgotpasswordController', 
      templateUrl: 'app/components/forgotpassword/forgotpasswordView.html', 
      controllerAs: 'vm' 
     }) 
     .when('/directory/:directoryType', { 
      controller: 'DirectoryController', 
      templateUrl: 'app/components/directory/directoryView.html', 
      resolve: {notifications:countAllUnreadsn,},  
      controllerAs: 'vm' 
     }) 
     .when('/home', { 
      controller: 'HomeController', 
      templateUrl: 'app/components/home/homeView.html', 
      resolve: {notifications:countAllUnreadsn,},   
      controllerAs: 'vm'   
     }) 
     .when('/myprofile', { 
      controller: 'MyProfileController', 
      templateUrl: 'app/components/profile/myProfileView.html', 
      resolve: {notifications:countAllUnreadsn,},  
      controllerAs: 'vm' 
     }) 
     .otherwise({ 
      redirectTo: '/login' 
     }); 
    }]); 

})(); 
+0

控制器在哪裏? – Subash

+0

控制器都在那裏。這是路由發生的地方的文件,我使用resolve從這裏發送一個http調用。因此,儘管每個頁面都有控制器,但這裏不需要控制器。爲了減少歧義,我沒有包含它們。 @Subash – Annabelle

+0

'$ scope'在resolve方法裏面不會有效,它應該只返回promise的一個期望的結果 –

回答

3

問題是,您在加載通知的函數中使用$ scope。 $範圍將不可用,因爲它可能尚未創建。您需要返回一個承諾,並將解析的值作爲依賴項注入控制器。

var countAllUnreads = function($location, $q, AuthService3) 
    { 
     var deferred = $q.defer(); 
     AuthService3.fetchUnreadNotifications().then(function (res) 
     {  
     deferred.resolve(res.data.totalUnread); 
     }); 

     return deferred.promise; 
    }; 

而在您的控制器中,有'通知'的依賴關係。

Ex: function HomeController($scope, $http, notifications){ } 
+0

你對通知的依賴是什麼意思?應該是服務還是電話。我猜不會。或者,有沒有一種方法可以將其存儲在控制器中? @Sarathy – Annabelle

+0

不知道你如何聲明你的控制器。就像$ http,$ scope一樣,您需要爲控制器添加一個名爲'notifications'的附加參數,該參數應該與路由配置中爲'resolve'提供的屬性名稱匹配。看看我在答案中提供的HomeController的例子。您在方法countAllReads中的服務調用的結果將由Angular注入爲「通知」。無需任何額外的服務電話。 – Sarathy

+0

看看Scott Allen的這篇文章。它應該有所幫助。 http://odetocode.com/blogs/scott/archive/2014/05/20/using-resolve-in-angularjs-routes.aspx – Sarathy

相關問題