filmov
tv
AngularJS controller as syntax

Показать описание
controller as syntax not working with promises
controller as syntax not working $http
controller as syntax not working then
controller as syntax angularjs
angularjs controller as example
angularjs controlleras syntax
angularjs controlleras example
In this video we will discuss AngularJS controller as syntax. This is continuation to Part 31. Please watch Part 31 from AngularJS Tutorial before proceeding.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
So far in this video series we have been using $scope to expose the members from the controller to the view.
});
In the example above we are attaching message property to $scope, which is automatically available in the view.
[h1 ng-controller="mainController"]{{message}}[/h1]
Another way that is available to expose the members from the controller to the view, is by using CONTROLLER AS syntax. With this syntax, there is no need to inject $scope object in to the controller function, instead you simply use this keyword as shown below.
});
In the view, you use, CONTROLLER AS syntax as shown below.
Now, let us see how we can use this CONTROLLER AS syntax in the example that we worked with in Part 31.
1. In the when() function, notice that we are using CONTROLLER AS syntax
2. With in each controller() function we are using this keyword to set the properties that we want to make available in the view
3. Notice in studentsController and studentDetailsController we are assigning this keyword to variable vm. vm stands for ViewModel. You can give it any meaningful name you want.
4. If you use this keyword in then() function as shown below, you would not get the result you expect. That's because 'this' keyword points to the window object when the control comes to then() function.
.controller("studentsController", function ($http) {
.then(function (response) {
})
})
At this point we also need to modify all our partial templates
[h1]Courses we offer[/h1]
[ul]
{{course}}
[/li]
[/ul]
[h1]List of Students[/h1]
[ul]
[/a]
[/li]
[/ul]
[h1]Student Details[/h1]
[table style="border:1px solid black"]
[tr]
[td]Id[/td]
[/tr]
[tr]
[td]Name[/td]
[/tr]
[tr]
[td]Gender[/td]
[/tr]
[tr]
[td]City[/td]
[/tr]
[/table]
[h4][a href="students"]Back to Students list[/a][/h4]
You can also use CONTROLLER AS syntax when defining routes as shown below
var app = angular
.module("Demo", ["ngRoute"])
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when("/home", {
controller: "homeController",
controllerAs:"homeCtrl"
})
})
In our next video we will discuss, how the CONTROLLER AS syntax can make our code more readable as opposed to using $scope when working with nested scopes.
Link for all dot net and sql server video tutorial playlists
Link for slides, code samples and text version of the video
controller as syntax not working $http
controller as syntax not working then
controller as syntax angularjs
angularjs controller as example
angularjs controlleras syntax
angularjs controlleras example
In this video we will discuss AngularJS controller as syntax. This is continuation to Part 31. Please watch Part 31 from AngularJS Tutorial before proceeding.
Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
So far in this video series we have been using $scope to expose the members from the controller to the view.
});
In the example above we are attaching message property to $scope, which is automatically available in the view.
[h1 ng-controller="mainController"]{{message}}[/h1]
Another way that is available to expose the members from the controller to the view, is by using CONTROLLER AS syntax. With this syntax, there is no need to inject $scope object in to the controller function, instead you simply use this keyword as shown below.
});
In the view, you use, CONTROLLER AS syntax as shown below.
Now, let us see how we can use this CONTROLLER AS syntax in the example that we worked with in Part 31.
1. In the when() function, notice that we are using CONTROLLER AS syntax
2. With in each controller() function we are using this keyword to set the properties that we want to make available in the view
3. Notice in studentsController and studentDetailsController we are assigning this keyword to variable vm. vm stands for ViewModel. You can give it any meaningful name you want.
4. If you use this keyword in then() function as shown below, you would not get the result you expect. That's because 'this' keyword points to the window object when the control comes to then() function.
.controller("studentsController", function ($http) {
.then(function (response) {
})
})
At this point we also need to modify all our partial templates
[h1]Courses we offer[/h1]
[ul]
{{course}}
[/li]
[/ul]
[h1]List of Students[/h1]
[ul]
[/a]
[/li]
[/ul]
[h1]Student Details[/h1]
[table style="border:1px solid black"]
[tr]
[td]Id[/td]
[/tr]
[tr]
[td]Name[/td]
[/tr]
[tr]
[td]Gender[/td]
[/tr]
[tr]
[td]City[/td]
[/tr]
[/table]
[h4][a href="students"]Back to Students list[/a][/h4]
You can also use CONTROLLER AS syntax when defining routes as shown below
var app = angular
.module("Demo", ["ngRoute"])
.config(function ($routeProvider, $locationProvider) {
$routeProvider
.when("/home", {
controller: "homeController",
controllerAs:"homeCtrl"
})
})
In our next video we will discuss, how the CONTROLLER AS syntax can make our code more readable as opposed to using $scope when working with nested scopes.
Link for all dot net and sql server video tutorial playlists
Link for slides, code samples and text version of the video
Комментарии