2016-12-28 66 views
0

在我正在處理的項目中,我們在$rootScope上有一個變量,名爲events。我可以在我的控制器中使用$rootScope.events將其注入到我的控制器後訪問它。AngularJS在設置時獲取rootScope變量

服務設置$rootScope變量上的事件可能需要一些時間。現在我要添加一個需要變量第一個事件ID的新功能。問題是,在設置了$rootScope.events之前會調用它。在設置了$rootscope.events後,我無法弄清楚如何在控制器中調用該方法。我之前使用過$watch,但它似乎並沒有在這個變量上工作。該代碼我想:

$scope.$watch('$rootScope.events', function() { 
    if ($rootScope.events.length > 0) { 
     getDetails(); // function I want to call after $rootscope.events is set 
     $log.debug($rootScope.events); // debugging line 
    } 
}); 

我加入了$rootScope.events.length > 0,以避免它得到一個無限循環。不知道這是否有必要。有什麼我需要添加此功能的解決方案?像這樣的手錶?或者我做錯了什麼?

我不認爲你需要更多的代碼,然後我添加到這篇文章,因爲我剛剛在我的控制器中注入$scope$rootScope,然後$log.debug()應該用set變量調用。目前它返回一個空變量。如果我錯了,請在評論中告訴我。

回答

0

它已經一段時間,但我想你想的:

$rootScope.$watch('events', function() { 
    if ($rootScope.events.length > 0) { 
     getDetails(); // function I want to call after $rootscope.events is set 
     $log.debug($rootScope.events); // debugging line 
    } 
}); 

events$rootscope值,但$rootscope.events$scope的值。

爲了避免混亂$rootscope手錶帶,但是,你應該使用:

$scope.$watch('$root.events', function() { 
    var events = $scope.$root.events; 
    if (events.length > 0) { 
     getDetails(); // function I want to call after events is set 
     $log.debug(events); // debugging line 
    } 
}); 

或者乾脆:

$scope.$watch('$root.events', function(newValue, oldValue) { 
    if (newValue.length > 0) { 
     getDetails(); // function I want to call after events is set 
     $log.debug(newValue); // debugging line 
    } 
}); 
+0

Wauww我很親密。這更有意義。謝謝! – JeroenE

+0

@JeroenE這應該使用'$ scope。$ watch'並且更安全。如果放在控制器中並且當前的路由/狀態改變,$ rootScope的手錶可能不會被清除。每當您傳入此代碼時,都會留下一個/多個監視,以減緩應用程序的運行速度。 – Walfrat

+0

@JeroenE可以使用'$ scope。$ watch('$ root.events',...)'來解決這個問題。 – bugged87

0

RootScope documentation

觀看是一個字符串(範圍變量)或一個函數。

$scope.$watch(function() { 
    return $rootScope.events; 
}, function() { 
    console.log($rootScope.events); 
}, true); 
+0

謝謝你,這個工程很大,但什麼是「返回$ rootScope.events「? – JeroenE

+0

它是值函數參數。看到這個[教程](http://tutorials.jenkov.com/angularjs/watch-digest-apply.html#watch)。它基本上是一個重載,它期望一個返回值的函數,而不是一個命名該值的字符串。 – bugged87

+0

另請參閱$ watch文檔[此處](https://docs.angularjs.org/api/ng/type/$rootScope.Scope)。 – bugged87

0

而不是把一塊手錶爲發生一次的東西,你可以使用當事件準備就緒時,你的事件服務將會解決。

Event Service : ($rootScope & $q injected) 
    // in constructor : 
    this.deferred = $q.defer(); 
    $rootScope.eventPromise = deferred.promise; 


    // in a setup fonction or even within the constructor 
    setupEvent : function(){ 
     .. doing some stuff .. 
     .. somewhere in a asynchronous call back : 
      $rootScope.events =... //setup events 
      this.deferred.resolve();// or me.deferred using var me=this if some closure trouble 

     .. somewhere else if it fails .. 
      this.deferred.reject(); 
    } 

現在,讓我們可以肯定這將運行任何控制器將被加載之前:

angular.run(['EventService', function(EventService){ 
    // if you do everything in the constructor let the angular.run and don't run any code, 
    // this will make sure your events will start loading before angular will resolve the current routes. 
    EventService.setupEvent(); 
}]); 

現在讓我們使用它:

$rootScope.eventPromise.then(function(){ 
    $rootScope.events // we're safe here. 
});