2016-06-26 124 views
0

我有一個控制器內的函數,我很困惑如何更新.then函數內的$ scope變量。

我的繼承人功能:

searchSpotify(query, $scope) { 
    this.Spotify.search(query,'track').then(function (data) { 
     console.log(data.tracks.items); // this is working 
     $scope.result = data; 
    }); 
} 

在控制檯日誌中我收到此錯誤:

TypeError: Cannot set property 'result' of undefined 

我用$範圍$莫名其妙申請?

編輯:

這裏的上下文

(function() { 

    class SelectionController { 
     constructor(User, groupService, selectionService, songService, $scope, $routeParams, Spotify) { 
      this.groupId = $routeParams.groupId; 
      this.selectionId = $routeParams.selectionId; 
      this.groupService = groupService; 
      this.selectionService = selectionService 
       //this.selections = this.selectionService.getSelections(); 
      this.songService = songService; 
      this.searchResults; 
      this.$scope = $scope; 
      this.Spotify = Spotify 
     } 

     searchSpotify(query) { 
      this.Spotify.search(query, 'track').then(function(data) { 
       console.log(data.tracks.items); 
       $scope.result = data; 
      }); 
     } 

     addSong(name, artist, album) { 
      alert('called from selection controller'); 
      this.songService.addSong(this.selectionId, name, artist, album); 
     } 

     createSelection(name, description) { 
      this.selectionService.createSelection(this.groupId, name, description); 
     } 

     deleteSelection(selection) { 
      this.selectionService.deleteSelection(selection); 
     } 
    } 

    angular.module('songSelectionApp') 
     .controller('SelectionController', SelectionController); 

})(); 
+1

如果該功能位於控制器,不將$ scope變量傳遞給它。 – Noppey

+0

由'then'調用的函數作爲不同的範圍。如果你使用的是ES6,你可以使用箭頭函數,或者當我不通過$ scope變量時使用'bind()' – rpadovani

+0

,我在控制檯中得到以下錯誤:「ReferenceError:$ scope is not defined」 – Jcr

回答

2

保存參考$範圍:

searchSpotify(query) { 
     var $scope = this.$scope; 
     this.Spotify.search(query, 'track').then(function(data) { 
      console.log(data.tracks.items); 
      $scope.result = data; 
     }); 
    } 

或者使用arrow functions

searchSpotify(query) { 
     this.Spotify.search(query, 'track').then((data) => { 
      console.log(data.tracks.items); 
      this.$scope.result = data; 
     }); 
    } 

.bind(this)也應該工作,作爲另一個答案建議。

關於$scope.$apply:你需要它僅當要在使用外部(非角度的)庫/功能改變$scope$digest的,例如,像WebSocket,或jquery的事件偵聽器。直到Spotify是角服務/工廠 - 你不需要$scope.$apply

docs

$apply() is used to execute an expression in angular from outside of the angular framework. (For example from browser DOM events, setTimeout, XHR or third party libraries).

1
this.Spotify.search(query,'track').then(function (data) { 
    console.log(data.tracks.items); // this is working 
    this.result = data; 
}.bind($scope)); 

應該是所有你真的需要整個控制器。你基本上是告訴內部範圍是什麼this確實應該

+0

我想,'$ scope'是未定義的。你的例子將工作,如果你通過'.bind(this。$ scope)' –

+0

我不得不使用.bind(this。$ scope)來讓這個選項起作用 – Jcr

1

您應該使用

this.Spotify.search(query,'track').then(function (data) { 
    console.log(data.tracks.items); // this is working 
    this.$scope.result = data; 
}.bind(this));