4

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.

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
  • 2
    Could you give a complete example of what you mean? I'm not sure I follow you. A controller is instanctiated by angular, and exposes as many functions it wants in the scope in order for the view to be able to call these functions. – JB Nizet May 06 '14 at 19:59
  • ok, I'll give it a shot. Just a sec. Also I understand that you can throw any number of behavior methods, which can be utilized to great effect. But the controllers I see in other MVC frameworks appear to be a little different. I'll see if my example helps. – Mark Rogers May 06 '14 at 20:02
  • Sorry it took me a while to respond because the stack site went into read-only mode. – Mark Rogers May 06 '14 at 21:02
  • Hey what's with the downvote? How could that possibly be justified? – Mark Rogers May 06 '14 at 21:31
  • Angular controllers do not define routes, if that's what you're asking. Routes are defined elsewhere. but you can associate multiple routes to a single controller if you want to. The same constructor will always be used though (but with potentially different arguments). – JB Nizet May 07 '14 at 06:00
  • I think I understand now, AngularJS simply does not have action methods. – Mark Rogers May 07 '14 at 14:16
  • @MarkRogers Controllers can have functions appended to `$scope` but as for picking which one to execute based on the `$routeParams` sounds like a routing controller or https://github.com/angular-ui/ui-router – Nate-Wilkins Jun 28 '14 at 16:47

1 Answers1

1

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

Mark Rogers
  • 96,497
  • 18
  • 85
  • 138
  • @Rogers, Hey your question is absolutely meaningful for those who have come from other MVC frameworks. So I need to know, how you achieved this 'action' in 'contoller' , OR juset followed like one controller for one view ? – RONE Jun 02 '14 at 09:57
  • In the controller definition function, it appears that you wire up whatever behavior is going to occur on the page, and in various places you can define what routes a controller has. So you can have multiple views per controller, but you mostly just wire up "actions" you need by passing back some javascript functions attached to the scope. Those functions are then used by the view, wherever it might need it. – Mark Rogers Jun 02 '14 at 14:03