AngularJS控制器会使用多个action/setup方法吗?



我所见过的大多数AngularJS控制器的例子,通常都有一个单一的动作方法来为视图连接所有的东西。另一方面,在使用MVC模式(而不是AngularJS的MVW)的控制器中,每个控制器通常有多个动作方法,但在AngularJS中似乎不是这样。

如果可以将任意数量的执行行为的方法连接到$作用域(或其他对象)中,这似乎与MVC的动作方法相同,因为它们不会自动接受直接路由输入。

我很感兴趣,因为我正试图将现有的Asp.net MVC应用程序转换为angular,我正试图决定控制器的最佳组织分解。

我的各种假设是正确的吗?

AngularJS控制器会使用多个action/setup方法吗?

角控制器被分解成单独的动作吗?或者一个角控制器有或多或少一个动作,尽管路由和视图可能不同?


:
请求的示例- AngularJS控制器:

myApp.controller('DoubleController', ['$scope', function($scope) {
  $scope.double = function(value) { return value * 2; };
}]);

Asp。Net Controller MVC示例:

public class CardController : Controller
    {
        private readonly IService _service;
        public CardController(IService service)
        {
            _service = service;
        }
        public ActionResult Index(Guid gameId)
        {
            var model = _service.GenerateCardBuyDisplayModel(gameId);
            return View(model);
        }
        public ActionResult BuyCards(ExecuteBuyModel input)
        {
            _service.ExecuteBuy(input.GameId, input.CardsToBuy);
            return RedirectToAction("Index", "Game", new { id = input.GameId});
        }
    }

Ruby on Rails控制器示例:

class ClientsController < ApplicationController
  # This action uses query string parameters because it gets run
  # by an HTTP GET request, but this does not make any difference
  # to the way in which the parameters are accessed. The URL for
  # this action would look like this in order to list activated
  # clients: /clients?status=activated
  def index
    if params[:status] == "activated"
      @clients = Client.activated
    else
      @clients = Client.inactivated
    end
  end
  # This action uses POST parameters. They are most likely coming
  # from an HTML form which the user has submitted. The URL for
  # this RESTful request will be "/clients", and the data will be
  # sent as part of the request body.
  def create
    @client = Client.new(params[:client])
    if @client.save
      redirect_to @client
    else
      # This line overrides the default rendering behavior, which
      # would have been to render the "create" view.
      render "new"
    end
  end
end

如果你看这三个例子,AngularJS只有一个构造函数/setup方法,而Asp.net MVC的例子有一个构造函数和两个动作方法。Ruby of rails的例子甚至没有一个可见的构造函数,只有动作方法。Asp.net MVC示例(或Ruby on Rails示例)类似于其他MVC实现中的许多操作。在AngularJS中,我猜只有一个动作/构造函数方法,其中一个会附加任何额外的行为。另一方面,Asp.net MVC示例有一个构造函数和两个操作方法,它们都可以以不同的方式路由到。类似于AngularJS的单个构造器/动作。

据我所知,AngularJS控制器不像MVC控制器那样有传统的动作方法。相反,无论控制器能做什么,都必须在单个构造函数方法中定义,或者在应用程序配置中使用路由进行配置。如果你需要一个不同于构造函数的设置,那么它可能是一个使用新控制器的好地方。

后来我在角IRC聊天室问:

Angular控制器并不像在其他MVC实现如Ruby on Rails或Asp.net MVC?对吧?

wafflejock回应:

基本上没有什么需要或给予的

只是一个没有"基础"对象可以扩展的骨架结构从或接口实现

robdubya also mention:

除非你想要未来的性感https://gist.github.com/robwormald/bc87cb187e8f96c4e5f0

最新更新