kris kaman
kris kaman

Reputation: 101

Why does the Angular JS Route require you to cite a controller?

Why does the Angular JS Route require you to cite a controller?

Coulnd't you just cite a view and then have the view directive link to to the controller?

Upvotes: 1

Views: 133

Answers (1)

Tims
Tims

Reputation: 2007

Why does the Angular JS Route require you to cite a controller?

route does not require a controller to be specified. If you omit the controller it will just load the specified page but you will have limited functionality.

Coulnd't you just cite a view and then have the view directive link to to the controller?

No. This is not what directives are for. Quoting from the docs for directives:

What are Directives?

At a high level, directives are markers on a DOM element (such as an attribute, element name, or CSS class) that tell AngularJS's HTML compiler ($compile) to attach a specified behavior to that DOM element or even transform the DOM element and its children.

...and the docs for controllers:

Understanding Controllers

In Angular, a Controller is a JavaScript constructor function that is used to augment the Angular Scope.

When a Controller is attached to the DOM via the ng-controller directive, Angular will instantiate a new Controller object, using the specified Controller's constructor function. A new child scope will be available as an injectable parameter to the Controller's constructor function as $scope.

Use Controllers to:

  • Set up the initial state of the $scope object.
  • Add behavior to the $scope object.

Upvotes: 3

Related Questions