So far in this video series we have been using $scope to expose the members from the controller to the view.

app.controller("mainController", function ($scope) {
$scope.message = "Hello Angular";
});

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.

app.controller("mainController", function () {
this.message = "Hello Angular";
});

In the view, you use, CONTROLLER AS syntax as shown below.

<h1 ng-controller="mainController as main">{{main.message}}</h1>

Now, let us see how we can use this CONTROLLER AS syntax in the example that we worked with in Part 31.

Code changes in script.js. Notice the changes highlighted in yellow.
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) {
$http.get("StudentService.asmx/GetAllStudents")
.then(function (response) {
this.students = response.data;
})
})

At this point we also need to modify all our partial templates

Changes in home.html : Use homeCtrl object to retrieve the message property that the homeController has set.

<h1>{{homeCtrl.message}}</h1>

<div>
PRAGIM Established in 2000 by 3 S/W engineers offers very cost effective training. PRAGIM Speciality in training arena unlike other training institutions
</div>
<ul>
<li>Training delivered by real time software experts having more than 10 years of experience.</li>
<li>Realtime projects discussion relating to the possible interview questions.</li>
<li>Trainees can attend training and use lab untill you get a job.</li>
<li>Resume preperation and mock up interviews.</li>
<li>100% placement assistance.</li>
<li>Lab facility.</li>
</ul>

Changes in courses.html : Use coursesCtrl object to retrieve the courses property that the coursesController has set.

<h1>Courses we offer</h1>
<ul>
<li ng-repeat="course in coursesCtrl.courses">
{{course}}
</li>
</ul>

Changes in students.html : Use studentsCtrl object to retrieve the students property that the studentsController has set.

<h1>List of Students</h1>
<ul>
<li ng-repeat="student in studentsCtrl.students">
<a href="students/{{student.id}}">
{{student.name}}
</a>
</li>
</ul>

Changes in studentDetails.html : Use studentDetailsCtrl object to retrieve the student property that the studentDetailsController has set.

<h1>Student Details</h1>
<table style="border:1px solid black">
<tr>
<td>Id</td>
<td>{{studentDetailsCtrl.student.id}}</td>
</tr>
<tr>
<td>Name</td>
<td>{{studentDetailsCtrl.student.name}}</td>
</tr>
<tr>
<td>Gender</td>
<td>{{studentDetailsCtrl.student.gender}}</td>
</tr>
<tr>
<td>City</td>
<td>{{studentDetailsCtrl.student.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", {
templateUrl: "Templates/home.html",
controller: "homeController",
controllerAs: "homeCtrl"
})
.when("/courses", {
templateUrl: "Templates/courses.html",
controller: "coursesController as coursesCtrl",
controllerAs: "coursesCtrl"
})
.when("/students", {
templateUrl: "Templates/students.html",
controller: "studentsController as studentsCtrl",
controllerAs: "studentsCtrl"
})
.when("/students/:id", {
templateUrl: "Templates/studentDetails.html",
controller: "studentDetailsController as studentDetailsCtrl",
controllerAs: "studentDetailsCtrl"
})
.otherwise({
redirectTo: "/home"
})
$locationProvider.html5Mode(true);
})

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.

Part 32 AngularJS controller as syntax的更多相关文章

  1. Angularjs Controller 间通信机制

    在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...

  2. 【转】Angularjs Controller 间通信机制

    在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...

  3. (十六)JQuery Ready和angularJS controller的运行顺序问题

    项目中使用了JQuery和AngularJS框架,近期定位一个问题,原因就是JQuery Ready写在了angularJS controller之前,导致JQuery选择器无法选中须要的元素(由于a ...

  4. Part 34 AngularJS controller as vs scope

    There are 2 ways to expose the members from the controller to the view - $scope and CONTROLLER AS. T ...

  5. Part 33 Angular nested scopes and controller as syntax

    Working with nested scopes using $scope object : The following code creates 3 controllers  - country ...

  6. angularjs controller 继承

    前沿 最近在angularjs项目当中,看到 controller 好多都是重复性的代码,在 controller 当中有好多代码很相似 function(比如 controller 下的 CRUD ...

  7. Angularjs Controller间通信的几种方法

    先说最简单的,适合简单数据 一.使用controller as <body ng-controller="ParentCtrl as parent"> <inpu ...

  8. angular controller as syntax vs scope

    今天要和大家分享的是angular从1.2版本开始带来了新语法Controller as.再次之前我们对于angular在view上的绑定都必须使用直接的scope对象,对于controller来说我 ...

  9. AngularJS 'Controller As'用法

    AngularJS 1.2版本中提供了Controller As语法,简单说就是可以在Controller中使用this来替代$scope,使得Controller更像一个传统的JS类,相对于$sco ...

随机推荐

  1. P4389-付公主的背包【生成函数,多项式exp】

    正题 题目链接:https://www.luogu.com.cn/problem/P4389 题目大意 \(n\)种物品,第\(i\)种大小为\(v_i\),数量无限.对于每个\(s\in[1,m]\ ...

  2. Python - poetry(1)包管理利器的入门介绍

    Python 虚拟环境详解 https://www.cnblogs.com/poloyy/p/15266382.html poetry 官方介绍 github:https://github.com/p ...

  3. 那些我用Windows时必备的软件

    那些我用Windows时必备的软件 使用Windows多年来,自己发现了许多不错的小软件,在此做一个备份清单,方便今后装新机. 起源 \(hadow\)ock$ 越过高墙的小飞机 Google Chr ...

  4. SphereEx 创始人张亮云咖访谈回顾:构建数据服务的新思路

    2021 年 7 月 21 日,2021 亚马逊云科技中国峰会在上海盛大开幕.本次大会以"构建新格局,共赢云时代"为主题,邀请到来自技术社区.开源软件基金会.开源创业代表.女性开发 ...

  5. SQL Server链接服务器信息查询

    exec sp_helpserver --查询链接服务器select * from sys.servers --查询链接服务器链接地址

  6. js 改变this指向的三种方法 bind call apply

    先了解下bind call apply 的注意点 bind 需要手动调用 第一个参数 this 要指向的对象,后面是 散列的参数 call 不需要手动调用 第一个参数 this 要指向的对象,后面是 ...

  7. 技术番外篇丨Github Action CI/CD

    起源 看到.Net群里再聊CI/CD,我就这里分享一下我目前自己一些小东西的做法,我目前在Github有一个自己私有的组织,里面存放了我的部分商业化项目,早期我采用Jenkins用Webhooks进行 ...

  8. iOS自定义拍照框拍照&裁剪(一)

    卡片机时代 很重要的一点是,相机本身是没有方向概念的,它不理解拍摄的内容,只会以相机自己的坐标系去保存数据,下图展示了相机对"F"进行四个角度拍摄时返回的图片数据. 最初的卡片机时 ...

  9. Java初步学习——2021.10.05每日总结,第五周周二

    (1)今天做了什么: (2)明天准备做什么? (3)遇到的问题,如何解决? 今天学习了二维数组,包括二维数组的声明,和二维数组的创建.以及获取二维数组的长度,其中要注意的是二维数组是每个元素都是一维数 ...

  10. WEB前端工程师如何做职业规划?

    对于一个WEB前端的职业规划,其实是有各种的答案,没有哪种答案是完全正确的,全凭自己的选择,只要是自己选定了,坚持去认真走,就好.在这里, 我只是 简要说一下自己对于这块儿内容的理解.有一个观点想要分 ...