2014-03-28 86 views
1

我有一個角度的應用程序需要連接到服務器,並從他們授權,然後我們檢索數據 問題是{{}}作用域不顯示從服務器檢索更新的數據。在我的情況下如何在運行時填充數據?

我有類似

.controller('Ctrl', function($scope, $rootScope) { 
     'use strict'; 
     $scope.signIn = function() {   
      // Authenticate (takes a bout 2 second to get the data back 
      server1.authenticate(uame, pd, function(data){ 
      //parse data 
      $scope.data = data 
      }    
      $scope.$broadcast('sData', $scope); 
     } 
}) 
.controller('mCtrl', function($scope, $rootScope) { 
     'use strict'; 
     $scope.test='old test'; 
     $scope.$on('sData', function(event, nData){ 
      $scope.test=nData.test //show 'new test' as a string 
     }) 
}) 

HTML

<div ng-click='signIn'>click me</div> 
{{test}} //shows 'old test' when first load. but not showing 'new test' after user click the div. 

誰能幫我一下嗎?非常感謝!

+0

你可以嘗試event.currentScope.test = nData.test – jOshT

+0

你可能需要使用$ scope。$ apply() –

回答

2

這是從文檔:http://docs.angularjs.org/api/ng/type/ $ rootScope.Scope

這可能是由於消化已經發生了。我要麼使用:

$scope.$apply() 
//or better 
//use the scope returned in the listener 
$scope.$on('sData', function(event, nData){ 
    event.currentScope.test = nData.test //show 'new test' as a string 
}) 

$的(名字,聽衆); 偵聽給定類型的事件。請參閱$ emit討論事件生命週期。

事件監聽器函數格式爲:函數(event,args ...)。傳入偵聽器的事件對象具有以下屬性:

targetScope - {Scope}:事件爲$ emit-ed或$ broadcast-ed的作用域。

currentScope - {Scope}:處理事件的當前作用域。

name - {string}:事件的名稱。

stopPropagation - {功能=}:調用stopPropagation功能將取消進一步的事件 傳播(僅適用於爲$發射-ED事件)。 preventDefault - {function}:調用preventDefault將defaultPrevented標誌設置爲true。

defaultPrevented - {}布爾:如果爲true的preventDefault被調用。

相關問題