Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Controller as" syntax for ng-view

I was wondering how I use the Controller as syntax in combination with ngRoute since I cant do ng-controller="Controller as ctrl"

like image 381
user1703761 Avatar asked May 26 '14 00:05

user1703761


People also ask

What is Ng-controller in angular?

AngularJS ng-controller Directive The ng-controller directive adds a controller to your application. In the controller you can write code, and make functions and variables, which will be parts of an object, available inside the current HTML element. In AngularJS this object is called a scope.

How does an NG-controller work?

The ng-controller Directive in AngularJS is used to add a controller to the application. It can be used to add methods, functions, and variables that can be called on some event like click, etc to perform certain actions. Parameter value: expression: It refers to the name of the controller.

Which is the correct syntax of creating AngularJS controller?

AngularJS Example The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller. The myCtrl function is a JavaScript function. AngularJS will invoke the controller with a $scope object.

What is controller in AngularJS?

In AngularJS, a Controller is defined by a JavaScript constructor function that is used to augment the AngularJS Scope. Controllers can be attached to the DOM in different ways.


2 Answers

You can use the controller as syntax when you specify your controller in the $routeProvider configuration.

e.g.

$routeProvider     .when('/somePath', {         template: htmlTemplate,         controller: 'myController as ctrl'     }); 
like image 135
rob Avatar answered Sep 26 '22 00:09

rob


Or, you can specify a controller assigning like when you create a new directive using controllerAs.

    $routeProvider         .when('/products', {             templateUrl: 'partials/products.html',             controller: 'ProductsController',             controllerAs: 'products'         }); 
like image 38
martinjezek Avatar answered Sep 25 '22 00:09

martinjezek