简体   繁体   English

在AngularJS控制器之间共享代码/方法/功能

[英]Sharing code/method/function between AngularJS controllers

I have searched around. 我四处搜寻。 People talked mostly about sharing data between controllers using factory. 人们谈论的主要是使用工厂在控制器之间共享数据。 But in my case I would like to share code logic between controllers. 但在我的情况下,我想在控制器之间共享代码逻辑。

$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;
 });
}; 

I would like to share these two methods in two controllers. 我想在两个控制器中分享这两种方法。 And how do I pass in the $scope from two different controller to my share methods? 如何将$scope从两个不同的控制器传递到我的共享方法?

Thanks for any help. 谢谢你的帮助。

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);
  }
}

And then in your controller(s), you could call these methods 然后在您的控制器中,您可以调用这些方法

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;
    });
}

Create a postService, which you inject into all of the controllers you want. 创建一个postService,将其注入所需的所有控制器。 Then have the controllers manage the $scope changes and get the content itself from your service. 然后让控制器管理$ scope更改并从您的服务中获取内容。 Below should get you started... 下面应该让你开始......

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

app.controller('whateverController', function($scope, postService) {
  $scope.post = postService.updatePost();
}

Update - Example of how to bind $scope element to value from service 更新 - 如何将$ scope元素绑定到服务的值的示例

HTML: HTML:

<div>{{comment_error_messages()}}</div>

In your controller: 在你的控制器中:

$scope.comment_error_messages = function () {
   return postService.getErrorMessages()
};

And your service: 而你的服务:

var getErrorMessages = function () {
   ...
   return val;
}

Angularjs has provided us with Factories and Servicece, Factories are for business logic to be used in controllers and Services should contain common code that is to be used in multiple places ie controllers or factories. Angularjs为我们提供了工厂和服务,工厂用于控制器中的业务逻辑,服务应包含在多个地方使用的通用代码,即控制器或工厂。 This is the way how logic is to be shared inside Angularjs app. 这是在Angularjs应用程序中共享逻辑的方式。

Happy Helping! 快乐帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM