Sunday, 2 April 2017

How to use Controller using Alias in Routes ?


2 ways to use Controller Alias with Routes
  1. controller property with "as" syntax
  2. "controllerAs" property

Example JS 1

var app = angular.module("module1", ["ngRoute"]);

app.config( function ($routeProvider) {
   $routeProvider
   .when("/home", {
      templateUrl: "templates/home.html",
      controller: "homeController as homeCtrl"
   })
   .when(
"/trainings", {
      templateUrl: "templates/trainings.html",
      controller: "trainingController
as trainingCtrl"
   })
   .when(
"/students", {
      templateUrl: "templates/students.html",
      controller: "studentController as studentCtrl"
   })
});



Example JS 2

var app = angular.module("module1", ["ngRoute"]);

app.config( function ($routeProvider) {
   $routeProvider
   .when("/home", {
      templateUrl: "templates/home.html",
      controller: "homeController",

      controllerAs: "homeCtrl" 
   })
   .when(
"/trainings", {
      templateUrl: "templates/trainings.html",

      controller: "trainingController",
      controllerAs: "trainingCtrl"
   })
   .when(
"/students", {
      templateUrl: "templates/students.html",

      controller: "studentController",
      controllerAs: "studentCtrl"
   })
});



No comments:

Post a Comment

Note: only a member of this blog may post a comment.