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 ...
随机推荐
- USACO Section 4
前言 好久没更新这个系列了,最近闲的无聊写一下.有两题搜索懒得写了. P2737 [USACO4.1]麦香牛块Beef McNuggets https://www.luogu.com.cn/probl ...
- mybatis多种查询方法
1. 查询单行,对象,并封装成一个对象 Employee getEmpById(Integer id); <select id="getEmpById" resultType ...
- 关于mysql基础
早就想把自己的数据库基础巩固一下,然而一直没有时间,今天终于抽出时间对mysql数据库基础进行了学习与扩展. mysql与其他数据库的区别 Sqlite: 开源免费,体积小,单文件,没有进程.磁盘读性 ...
- MySQL优化之路
一.Mysql的存储原理 索引相关 本质 索引是帮助MySQL高效获取数据的排好序的数据结构 建索引,提高数据检索的效率,降低数据库的IO成本: 通过索引列对数据进行排序,降低数据排序的成本, ...
- Java-多态(上)
什么是多态 同一方法可以根据发送对象的不同而采取多种不同的行为方式 一个对象实际类型是确定的 但指向其引用类型却有很多 注意事项 多态是方法的多态 属性没有多态 父类和子类 有联系 类型转换异常 Cl ...
- 改头换面为哪般,最像Android的Windows——Win11升级安装体验
在过完了十一小长假之后,各位打工人.学僧党可期待的不仅仅是新一轮的工作,Windows11也在10月5日悄悄正式发布,正式版已经面向MSDN订阅用户开放下载. 作为微软金牌合作伙伴,本葡萄已在第一时间 ...
- 我在阿里巴巴做 Serverless 云开发平台
技术的成熟度源自大规模的实践,Java 领域,阿里将自身的实践源源不断的反哺给微服务技术体系:Node.js 领域,阿里正掀起了前所未有的前端革命浪潮,将实践反哺给 Serverless技术体系,并逐 ...
- 洛谷2805 [NOI2009]植物大战僵尸 (拓扑排序+最小割)
坚决抵制长题面的题目! 首先观察到这个题目中,我们会发现,我们对于原图中的保护关系(一个点右边的点对于这个点也算是保护) 相当于一种依赖. 那么不难看出这个题实际上是一个最大权闭合子图模型. 我们直接 ...
- 微软 SqlHelper代码、功能、用法介绍:高效的组件
数据访问组件SqlHelper数据访问组件是一组通用的访问数据库的代码,在所有项目中都可以用,一般不需要修改.本节使用的是Microsoft提供的数据访问助手,其封装很严密,且应用简单. 首先要先添加 ...
- IDA硬编码修改SO,重新打包APK绕过FRIDA反调试
该案例来自看雪 通过IDA修改SO 找到检测点 修改字符串 双击该字符串进入TEXT VIEW 选择 HEX VIEW 修改十六进制 右击EDIT 修改字符 右击 菜单栏-> edit -> ...