I was wondering how I use the Controller as
syntax in combination with ngRoute
since I cant do ng-controller="Controller as ctrl"
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.
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.
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.
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.
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' });
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' });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With