Популярное

Музыка Кино и Анимация Автомобили Животные Спорт Путешествия Игры Юмор

Интересные видео

2025 Сериалы Трейлеры Новости Как сделать Видеоуроки Diy своими руками

Топ запросов

смотреть а4 schoolboy runaway турецкий сериал смотреть мультфильмы эдисон
dTub
Скачать

Angular nested scopes and controller as syntax

Автор: kudvenkat

Загружено: 2016-03-15

Просмотров: 83964

Описание:

angularjs nested controllers scope
angularjs nested controllers parent scope
angularjs nested controllers example

In this 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. This is continuation to Part 32. Please watch Part 32 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.
   / @aarvikitchen5572  

Working with nested scopes using $scope object : The following code creates 3 controllers - countryController, stateController, and cityController. All of these have set name property on the $scope object.

var app = angular
.module("Demo", [])
.controller("countryController", function ($scope) {
$scope.name = "India";
})
.controller("stateController", function ($scope) {
$scope.name = "Maharashtra";
})
.controller("cityController", function ($scope) {
$scope.name = "Mumbai";
});

Now we want to display Country, State and City names in the view

To get the output as shown above, we will have the following HTML in our view. name property retrieves the correct value as expected. However, the code is bit confusing.

[div ng-controller="countryController"]
{{name}}
[div ng-controller="stateController"]
{{name}}
[div ng-controller="cityController"]
{{name}}
[/div]
[/div]
[/div]

Now let us say we want to display the names as along with parent names.

To achieve this modify the HTML in the view as shown below. Notice we are using $parent to get the name property value of the immediate parent controller. To get the name property value of the grand parent, we are using $parent.$parent. This can get very confusing if you have many nested controllers and as a result the code gets less readable.

[div ng-controller="countryController"]
{{name}}
[div ng-controller="stateController"]
{{$parent.name}} - {{name}}
[div ng-controller="cityController"]
{{$parent.$parent.name}} - {{$parent.name}} - {{name}}
[/div]
[/div]
[/div]

Let us see how things change when we use CONTROLLER AS syntax. First change the angular code to support CONTROLLER AS syntax. Notice we are not using $scope anymore with in our controllers, instead, we are using "this" keyowrd.

var app = angular
.module("Demo", [])
.controller("countryController", function () {
this.name = "India";
})
.controller("stateController", function () {
this.name = "Maharashtra";
})
.controller("cityController", function () {
this.name = "Mumbai";
});

With in the view, use CONTROLLER AS syntax. With this change, we are able to use the respective controller object and retrieve name property value. Now there is no need to juggle with $parent property. No matter how deep you are in the nested hierarchy, you can very easily get any controller object name property value. The code is also much readable now.

[div ng-controller="countryController as countryCtrl"]
{{countryCtrl.name}}
[div ng-controller="stateController as stateCtrl"]
{{countryCtrl.name}} - {{stateCtrl.name}}
[div ng-controller="cityController as cityCtrl"]
{{countryCtrl.name}} - {{stateCtrl.name}} - {{cityCtrl.name}}
[/div]
[/div]
[/div]

Next Video : We will discuss the difference between $scope and controller as syntax

Link for all dot net and sql server video tutorial playlists
https://www.youtube.com/user/kudvenka...

Link for slides, code samples and text version of the video
http://csharp-video-tutorials.blogspo...

Angular nested scopes and controller as syntax

Поделиться в:

Доступные форматы для скачивания:

Скачать видео mp4

  • Информация по загрузке:

Скачать аудио mp3

Похожие видео

array(10) { [0]=> object(stdClass)#4645 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "F6BIxHkiHjc" ["related_video_title"]=> string(32) "AngularJS controller as vs scope" ["posted_time"]=> string(19) "9 лет назад" ["channelName"]=> string(9) "kudvenkat" } [1]=> object(stdClass)#4618 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "6fAi8xDCq4w" ["related_video_title"]=> string(30) "AngularJS controller as syntax" ["posted_time"]=> string(19) "9 лет назад" ["channelName"]=> string(9) "kudvenkat" } [2]=> object(stdClass)#4643 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "7ukeEbM5eSM" ["related_video_title"]=> string(40) "Difference between $scope and $rootScope" ["posted_time"]=> string(19) "9 лет назад" ["channelName"]=> string(9) "kudvenkat" } [3]=> object(stdClass)#4650 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "qMQLnkTOkCo" ["related_video_title"]=> string(173) "Израиль vs Иран: чья армия сильнее? | «Железный купол», ПВО, МОССАД vs дроны, самолеты, прокси" ["posted_time"]=> string(22) "22 часа назад" ["channelName"]=> string(8) "varlamov" } [4]=> object(stdClass)#4629 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "WZN3cLhnWh8" ["related_video_title"]=> string(42) "О Зеленского вытерли ***" ["posted_time"]=> string(24) "18 часов назад" ["channelName"]=> string(27) "Анатолий Шарий" } [5]=> object(stdClass)#4647 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "O4Hyb5HtD3s" ["related_video_title"]=> string(103) "Что говорят СМИ Ирана о войне с Израилем (English subtitles) @Max_Katz" ["posted_time"]=> string(24) "19 часов назад" ["channelName"]=> string(19) "Максим Кац" } [6]=> object(stdClass)#4642 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "D4v_d95Nwew" ["related_video_title"]=> string(129) "⚡️Путин согласился на встречу с Зеленским || Срочное обращение Кремля" ["posted_time"]=> string(23) "7 часов назад" ["channelName"]=> string(23) "Время Прядко" } [7]=> object(stdClass)#4652 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "QWx6QBlpvns" ["related_video_title"]=> string(88) "1. Встреча на Патриарших. Мастер и Маргарита. Full HD" ["posted_time"]=> string(19) "1 год назад" ["channelName"]=> string(19) "NightHORROR_Channel" } [8]=> object(stdClass)#4628 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "ZJKxyxf1C9k" ["related_video_title"]=> string(127) "Россия вступает в войну на Ближнем Востоке? / Жёсткое заявление МИД РФ" ["posted_time"]=> string(20) "21 час назад" ["channelName"]=> string(10) "NEXTA Live" } [9]=> object(stdClass)#4646 (5) { ["video_id"]=> int(9999999) ["related_video_id"]=> string(11) "SEfAGnQURQs" ["related_video_title"]=> string(162) "Нужно ли писать юнит-тесты? Дебаты о TDD, моках и бережливом тестировании | Илья Ильиных | #45" ["posted_time"]=> string(25) "3 недели назад" ["channelName"]=> string(93) "Организованное программирование | Кирилл Мокевнин" } }
AngularJS controller as vs scope

AngularJS controller as vs scope

AngularJS controller as syntax

AngularJS controller as syntax

Difference between $scope and $rootScope

Difference between $scope and $rootScope

Израиль vs Иран: чья армия сильнее? | «Железный купол», ПВО, МОССАД vs дроны, самолеты, прокси

Израиль vs Иран: чья армия сильнее? | «Железный купол», ПВО, МОССАД vs дроны, самолеты, прокси

О Зеленского вытерли ***

О Зеленского вытерли ***

Что говорят СМИ Ирана о войне с Израилем (English subtitles) @Max_Katz

Что говорят СМИ Ирана о войне с Израилем (English subtitles) @Max_Katz

⚡️Путин согласился на встречу с Зеленским || Срочное обращение Кремля

⚡️Путин согласился на встречу с Зеленским || Срочное обращение Кремля

1. Встреча на Патриарших. Мастер и Маргарита. Full HD

1. Встреча на Патриарших. Мастер и Маргарита. Full HD

Россия вступает в войну на Ближнем Востоке? / Жёсткое заявление МИД РФ

Россия вступает в войну на Ближнем Востоке? / Жёсткое заявление МИД РФ

Нужно ли писать юнит-тесты? Дебаты о TDD, моках и бережливом тестировании | Илья Ильиных | #45

Нужно ли писать юнит-тесты? Дебаты о TDD, моках и бережливом тестировании | Илья Ильиных | #45

© 2025 dtub. Все права защищены.



  • Контакты
  • О нас
  • Политика конфиденциальности



Контакты для правообладателей: [email protected]