Part 32 AngularJS controller as syntax
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的更多相关文章
- Angularjs Controller 间通信机制
在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...
- 【转】Angularjs Controller 间通信机制
在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需 ...
- (十六)JQuery Ready和angularJS controller的运行顺序问题
项目中使用了JQuery和AngularJS框架,近期定位一个问题,原因就是JQuery Ready写在了angularJS controller之前,导致JQuery选择器无法选中须要的元素(由于a ...
- 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 ...
- Part 33 Angular nested scopes and controller as syntax
Working with nested scopes using $scope object : The following code creates 3 controllers - country ...
- angularjs controller 继承
前沿 最近在angularjs项目当中,看到 controller 好多都是重复性的代码,在 controller 当中有好多代码很相似 function(比如 controller 下的 CRUD ...
- Angularjs Controller间通信的几种方法
先说最简单的,适合简单数据 一.使用controller as <body ng-controller="ParentCtrl as parent"> <inpu ...
- angular controller as syntax vs scope
今天要和大家分享的是angular从1.2版本开始带来了新语法Controller as.再次之前我们对于angular在view上的绑定都必须使用直接的scope对象,对于controller来说我 ...
- AngularJS 'Controller As'用法
AngularJS 1.2版本中提供了Controller As语法,简单说就是可以在Controller中使用this来替代$scope,使得Controller更像一个传统的JS类,相对于$sco ...
随机推荐
- P6113-[模板]一般图最大匹配【带花树】
正题 题目链接:https://www.luogu.com.cn/problem/P6113 题目大意 给出一张无向图,求最大匹配. \(1\leq n\leq 10^3,1\leq m\leq 5\ ...
- 51nod1355-斐波那契的最小公倍数【min-max容斥】
正题 题目链接:http://www.51nod.com/Challenge/Problem.html#problemId=1355 题目大意 定义\(f_i\)表示斐波那契的第\(i\)项,给出一个 ...
- vue 移动端项目切换页面,页面置顶
之前项目是pc端是使用router的方式实现置顶的 //main.js router.afterEach((to, from, next) => { window.scrollTo(0, 0) ...
- 全套Java教程_Java基础入门教程,零基础小白自学Java必备教程 #011 # 第十一单元 String&ArrayList #
一.本单元知识点概述 (Ⅰ)知识点概述 二.本单元教学目标 (Ⅰ)重点知识目标 1.ArrayList集合的常用方法2.ArrayList存储数据和遍历数据3.String类的构造方法4.String ...
- 数据库MHA故障分析
一.故障分析 1.MHA故障以后是否正常:不正常 2.如果master恢复了?MHA还能自动恢复吗?:不能 3.主从恢复删除此文件 rm saved_master_binlog_from_192 ...
- Mysql双主双从高可用集群的搭建且与MyCat进行整合
1.概述 老话说的好:瞻前顾后.患得患失只会让我们失败,下定决心,干就完了. 言归正传,之前我们聊了Mysql的一主一从读写分离集群的搭建,虽然一主一从或一主多从集群解决了并发读的问题,但由于主节点只 ...
- 电脑(windows)端口被占用如何解决
问题: 今天在启动项目的时候,控制台提示"8080端口被占用",此时我并没有启动其他项目.那么8080端口被占用解决方法如下: 1.点击左下角"开始",在搜索框 ...
- 一次简单的SQL注入绕WAF
本人也是小白一枚,大佬请绕过,这个其实是六月份的时候做的,那时候想多点实战经验,就直接用谷歌搜索找了一些网站,这个是其中一个 1.目标网站 2.发现有WAF防护 3.判断存在注入 4.猜测了一下闭合为 ...
- T-SQL——关于XML类型
目录 0. 将结果集转化为XML格式 1. 列值拼接为字符串 2. 字符串转换为列值 3. 一些说明 参考 志铭-2021年10月23日 10:43:21 0. 将结果集转化为XML格式 测试数据 I ...
- UltraSoft - Beta - Postmortem事后分析
UltraSoft - Beta - PostMORTEM 设想和目标 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 解决的问题和定义都在[软软软]功能规格说明书 ...