控制器中未直接分配给范围的逻辑应该在它到达控制器之前在其他地方完成

Logic in the controller that isn't assigned directly to the scope should be fulfilled else where before it gets to the controller

 $scope.listValueChanged = function (name) {
 listId = [];
 $scope.listType= name;
 };

 if ($scope.contacts &&   $scope.contacts.length > 0) {
       var gsSelectedContacts =$scope.contacts;
      for (var i = 0; i < $scope.contacts.length; i++) {
         contactIds.push(gsSelectedContacts[i].id);
       }
 }

对于上面的代码,我从我的上级那里得到了评论,如下所示 控制器中没有直接分配给范围的逻辑应该在到达控制器之前在其他地方完成,然后应用到$scope。 我只在一个地方使用这种方法。真的吗,我真的需要将这个逻辑移动到服务中吗?

好吧,这取决于这里的用例是什么。如果你打算重用某些东西,最好为它创建一个 service 但如果你不打算重用或者代码不足以用作服务,那么你应该避免使用 service 。 Service 旨在使您的代码更有条理并提供更好的可用性,因此这取决于您的实际需求。

details on where to use service can be found here

尝试进行以下更改并将注释逻辑移至服务

在 listType

的服务中创建 getter 和 setter

js代码

  $scope.listType = '';
  $scope.listValueChanged = function(name) {
    listId = [];
    $scope.listType = name;
  };

  if ($scope.contacts && $scope.contacts.length > 0) {
    var contactIds = [];
    var gsSelectedContacts = $scope.contacts;
    //
    // this logic you can keep in service
    //
    $scope.contacts.map(function(contact) {
      contactIds.push(contact);
    });
  }

希望对您有所帮助