Javascript 在AngularJS控制器之间共享代码/方法/函数

Javascript 在AngularJS控制器之间共享代码/方法/函数,javascript,angularjs,Javascript,Angularjs,我到处找了找。人们大多谈论使用factory在控制器之间共享数据。但在我的例子中,我希望在控制器之间共享代码逻辑 $scope.updatePost = function(){ $http.get(sprintf("/posts/%s.json", post.id)). success(function(data, status, headers, config) { $scope.post = data; }) }; $scope.postComment = functi

我到处找了找。人们大多谈论使用factory在控制器之间共享数据。但在我的例子中,我希望在控制器之间共享代码逻辑

$scope.updatePost = function(){
  $http.get(sprintf("/posts/%s.json", post.id)).
  success(function(data, status, headers, config) {
    $scope.post = data;
  })
};

$scope.postComment = function(){
  if(!$scope.post.logged){
    window.location.href = "/users/sign_in";
  }

  var data = { post_id: $scope.post.id, content: $scope.content };
  $http.post("/comments", data).
  success(function(data, status, headers, config) {
    $scope.comment_error_messages = [];
    $scope.updatePost();
  }).error(function(data, status, headers, config) {
    $scope.comment_error_messages = data.errors;
 });
}; 
我想在两个控制器中分享这两种方法。如何将
$scope
从两个不同的控制器传递到我的共享方法


谢谢您的帮助。

创建一个postService,您可以将它注入到所有需要的控制器中。然后让控制器管理$scope更改,并从您的服务中获取内容本身。下面应该让你开始

app.factory('postService', function() {
   var updatePost = function...
   var postComment = function ...
}

app.controller('whateverController', function($scope, postService) {
  $scope.post = postService.updatePost();
}
更新-如何将$scope元素绑定到服务值的示例

HTML:

以及您的服务:

var getErrorMessages = function () {
   ...
   return val;
}
然后在控制器中,可以调用这些方法

app.controller('myController', myController);

myController.$inject = ['$scope', 'postService'];

function myController($scope, postService) {
  $scope.updatePost = function() {
     postService
       .updatePost($scope.post)
       .success(function(data, status, headers, config) {
          $scope.post = data;
       });
  }

  $scope.postComment = function(){
    // you could move this to the postService if you wanted
    if(!$scope.post.logged){
      window.location.href = "/users/sign_in";
    }

    postService
      .postComment($scope.post, $scope.content)
      .success(function(data, status, headers, config) {
         $scope.comment_error_messages = [];
         $scope.updatePost();
      })
      .error(function(data, status, headers, config) {
        $scope.comment_error_messages = data.errors;
    });
}

Angularjs为我们提供了工厂和服务CE,工厂用于控制器中使用的业务逻辑,服务应包含用于多个位置(即控制器或工厂)的通用代码。这就是逻辑在Angularjs应用程序中共享的方式


乐于助人

酷,如何在调用postService.postComment后设置
$scope.comment\u error\u messages
?@icn有很多方法来处理这个问题,但我总是在我的服务中创建一个helper func,并将控制器中的$scope元素绑定到它(我在回答中的编辑中抛出了一个例子)
var getErrorMessages = function () {
   ...
   return val;
}
app.factory('postService', postService);

postService.$inject = ['$http'];

function postService($http) {
  var updatePost = function(post) {
    return $http.get(sprintf("/posts/%s.json", post.id))
  }

  var postComment = function(post, content) {
    var data = { post_id: post.id, content: content };
    return $http.post("/comments", data);
  }
}
app.controller('myController', myController);

myController.$inject = ['$scope', 'postService'];

function myController($scope, postService) {
  $scope.updatePost = function() {
     postService
       .updatePost($scope.post)
       .success(function(data, status, headers, config) {
          $scope.post = data;
       });
  }

  $scope.postComment = function(){
    // you could move this to the postService if you wanted
    if(!$scope.post.logged){
      window.location.href = "/users/sign_in";
    }

    postService
      .postComment($scope.post, $scope.content)
      .success(function(data, status, headers, config) {
         $scope.comment_error_messages = [];
         $scope.updatePost();
      })
      .error(function(data, status, headers, config) {
        $scope.comment_error_messages = data.errors;
    });
}