Angularjs 在angular中使用$controller服务时视图未更新

Angularjs 在angular中使用$controller服务时视图未更新,angularjs,Angularjs,我有两个控制器。视图绑定到第一个CTRL。我试图在第二个函数中运行该函数,以便更新视图。函数运行,但视图未更新。有什么想法吗 <div>{{people.length}}</div> angular.module('myApp').controller('firstCtrl', function($scope, $http) { var self = this; $scope.people = []; function getData() {

我有两个控制器。视图绑定到第一个CTRL。我试图在第二个函数中运行该函数,以便更新视图。函数运行,但视图未更新。有什么想法吗

<div>{{people.length}}</div>



angular.module('myApp').controller('firstCtrl', function($scope, $http) {
  var self = this;
  $scope.people = [];


  function getData() {
    $http.get('/people').then(function(res) {
      $scope.people = res.data;
    });
  }

  getData();
  $scope.getData = getData;

  self.getData = function(){
               $scope.getData();
      };
      return self;

});




angular.module('myApp').controller('secondCtrl', function( $controller, $scope, $http) {

    var firstCtrl= $controller('firstCtrl', { $scope: $scope.$new() });
    firstCtrl.getData(); //This runs but view is not updated above.
});
{{people.length}
angular.module('myApp').controller('firstCtrl',function($scope,$http){
var self=这个;
$scope.people=[];
函数getData(){
$http.get('/people')。然后(函数(res){
$scope.people=res.data;
});
}
getData();
$scope.getData=getData;
self.getData=function(){
$scope.getData();
};
回归自我;
});
角度.module('myApp').controller('secondCtrl',function($controller,$scope,$http){
var firstCtrl=$controller('firstCtrl',{$scope:$scope.$new()});
firstCtrl.getData();//此操作将运行,但上面的视图未更新。
});

我认为您的代码在$scope上有一些问题。因此,不要直接在firstCtrl中传递数据。我将回调传递给getData函数,并将数据分配给回调中的$scope.people

这是正在工作的Plunker:

> http://plnkr.co/edit/QznxFL

我认为您的代码在$scope上有一些问题。因此,不要直接在firstCtrl中传递数据。我将回调传递给getData函数,并将数据分配给回调中的$scope.people

这是正在工作的Plunker:

> http://plnkr.co/edit/QznxFL

似乎您没有在第二个控制器中通过作用域传递数据不确定您的意思。我应该传递什么?看看这个插件:传递通过作用域获得的数据,比如$scope.data=firstCtrl.getData();谢谢我知道您在那里做了什么,但是我的工厂有ajax调用……所以当您从
firstCtrl
进行ajax调用时。这对您有帮助吗?似乎您没有在第二个控制器中通过作用域传递数据不确定您的意思。我应该传递什么?看看这个插件:传递通过作用域获得的数据,比如$scope.data=firstCtrl.getData();谢谢我知道您在那里做了什么,但是我的工厂有ajax调用……所以当您从
firstCtrl
进行ajax调用时。这对你有帮助吗?