Domanda

Most of the examples of AngularJS controllers that I have seen, usually have a single action method that wires everything up for the view. On the other hand, in controllers that use the MVC pattern, rather than AngularJS's MVW, there are usually multiple action methods per controller, but this does not appear to be the case with AngularJS.

Granted one can wire into the $scope (or some other object) any number of methods that execute behavior, still this does seem to be the same as MVC's action methods, since they do not automatically accept direct route input.

I'm interested because I'm trying to convert an existing Asp.net MVC app to angular and I'm trying to decide the best organizational breakdown for the controllers.

Are my various assumptions correct?

Do AngularJS Controllers ever use more than one action/setup method?

Are angular controllers ever broken down into individual actions? Or does an angular controller have more or less one action, though the routing and view might be different?


Update:
Requested Example - AngularJS controller:

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

Asp.Net Controller MVC example:

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 controller example:

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

If you look at these three examples, the AngularJS has only a single constructor/setup method, while the Asp.net MVC example has a constructor and two action methods The Ruby of rails example does not even have a visible constructor, simply action methods. The Asp.net MVC example (or the Ruby on Rails example) is similar to how many actions work in other MVC implementations. In AngularJS I guess there is only one action/constructor method, where in one would attach any additional behavior. The Asp.net MVC example on the other hand has both a constructor and two action methods that can both be routed to in different ways. similar to the single AngularJS contrustor/action.

È stato utile?

Soluzione

From what I can tell AngularJS controllers do not have traditional action methods, like MVC controllers. Instead whatever a controller can do must be defined in the single constructor method or configured using the routing in the application configuration. If you need a setup that is different from the constructor then its probably a good place to use a new controller.

Later I asked in the angular IRC chatroom:

Angular controllers don't really have action methods like they do in other MVC implementations like Ruby on Rails or Asp.net MVC? right?

to which wafflejock responded:

nothing is required or given to you basically

and

it's just the barebones structure with no "base" objects to extend from or interface to implement

robdubya also mentioned:

unless you want to get all future-sexy https://gist.github.com/robwormald/bc87cb187e8f96c4e5f0

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top