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 ...
随机推荐
- (一):细说贝叶斯滤波:Bayes filters
认知计算,还要从贝叶斯滤波的基本思想讲起,本文主要是对<Probabilistic Robotics>中贝叶斯滤波器部分的详细讲解. 这一部分,我们先回顾贝叶斯公式的数学基础,然后再来介绍 ...
- 一个关于MySQL指定编码实现的小坑
写在前面 环境:MySQL5.7+,MySQL数据库字符编码实现为utf8,表也为utf8 场景:微信授权获取用户信息(包括昵称)并保存到数据库,有的用户成功了,少数用户却失败了 那么为什么会失败呢? ...
- C语言的return语句
Q1:函数中的Return语句有什么用? Q2:Return 0有什么含义吗? A1:Return的作用为,跳出当前的函数,并且返回到调用当前函数的主调函数,当前函数中Return语句一下代码将不会运 ...
- JavaCPP快速入门(官方demo增强版)
欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...
- (课内)信安数基RSA-基础&&解密加速
RSA基本实现 首先获得N比特的伪随机数:使用Random库中内容. randint(n,m) 表示生成一个在n和m之间的随机数, **表示乘幂. getPrime找素数,or 1运算是一种优化:如果 ...
- Less-(38~41) 堆叠注入
首先申明,Less-(38~41)可以采取和Less-(1~4)相同的解法:(一一对应) 然而,他们的漏洞其实更大,我们可以做更多具有破坏性的事情. 代码审计: Less-(38~41): 41的$s ...
- 【UE4 设计模式】工厂方法模式 Factory Method Pattern 及自定义创建资源
概述 描述 又称为工厂模式,也叫虚拟构造器(Virtual Constructor)模式,或者多态工厂(Polymorphic Factory)模式 工厂父类负责定义创建产品对象的公共接口,而工厂子类 ...
- Linux argc,argv详解
来源:微信公众号「编程学习基地」 @ 目录 argc,argv是什么 如何解析程序参数 "选项"是什么? "选项字符串"是什么 解析参数 argc,argv是什 ...
- JVM:内存溢出OOM
JVM:内存溢出OOM 本笔记是根据bilibili上 尚硅谷 的课程 Java大厂面试题第二季 而做的笔记 经典错误 JVM 中常见的两个 OOM 错误 StackoverflowError:栈溢出 ...
- [no_code]团队任务拆解Alpha
项目 内容 这个作业属于哪个课程 2020春季计算机学院软件工程(罗杰 任健) 这个作业的要求在哪里 团队任务拆解 我们在这个课程的目标是 远程协同工作,采用最新技术开发软件 这个作业在哪个具体方面帮 ...