2016-06-26 91 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));