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.

时间: 2024-10-07 04:51:04

Part 32 AngularJS controller as syntax的相关文章

挖掘angularjs &quot;controller as&quot; 语法

原文:http://toddmotto.com/digging-into-angulars-controller-as-syntax/ AngularJS Controllers have recently gone under some changes (version 1.2 to be precise). What this means for scopes, Controllers and Angular development is some very subtle but power

angular controller as syntax vs scope

今天要和大家分享的是angular从1.2版本开始带来了新语法Controller as.再次之前我们对于angular在view上的绑定都必须使用直接的scope对象,对于controller来说我们也得必须注入$scope这个service.如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 angular.module("app",[]) .controller("demoController",["$scope&qu

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

项目中使用了JQuery和AngularJS框架,近期定位一个问题,原因就是JQuery Ready写在了angularJS controller之前,导致JQuery选择器无法选中须要的元素(由于angularJS controller还没有初始化,dom元素的class属性没有被加入).于是就引出了一个问题,jquery和angularjs谁先运行谁后运行的问题.当然最好我们编写的代码不要依赖于这样的顺序,依赖于某些顺序的代码更easy出错. <html> <head> <

JQuery Ready和angularJS controller的执行顺序问题

项目中使用了JQuery和AngularJS框架,最近定位一个问题,原因就是JQuery Ready写在了angularJS controller之前,导致JQuery选择器无法选中需要的元素(因为angularJS controller还没有初始化,dom元素的class属性没有被添加).于是就引出了一个问题,jquery和angularjs谁先执行谁后执行的问题.当然最好我们编写的代码不要依赖于这种顺序,依赖于某些顺序的代码更容易出错. <html> <head> <sc

Angularjs Controller

Angularjs Controller 使用过程中的注意点:1.不要试图去复用 Controller, 一个控制器一般只负责一小块试图.2.不要在Controller 中操作DOM, 这不是控制器的职责.3.不要在Controller 里面数据格式化,ng 有很好用的表单空间4. 不要在Controller 里面对数据进行过滤,ng 有$filter 服务5. Controller 是不会互相调用的,控制器之间的交互会通过事件进行.

【转】Angularjs Controller 间通信机制

在Angularjs开发一些经验总结随笔中提到我们需要按照业务却分angular controller,避免过大无所不能的上帝controller,我们把controller分离开了,但是有时候我们需要在controller中通信,一般为比较简单的通信机制,告诉同伴controller我的某个你所关心的东西改变了,怎么办?如果你是一个javascript程序员你会很自然的想到异步回调响应式通信—事件机制(或消息机制).对,这就是angularjs解决controller之间通信的机制,所推荐的唯

跟我学AngularJs:Controller数据共享、继承、通信使用具体解释

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主讲了AngularJs中的Controller中数据共享.继承.通信的具体使用 本教程使用AngularJs版本号:1.5.3 AngularJs GitHub: https://github.com/angular/angular.js/ AngularJs下载地址:https://angularjs.org/ 一.controller基础与使用方法 AngularJS中的co

AngularJS(三)——在多个AngularJS controller之间共享数据

在MVC中,为了方便维护,针对不同业务会使用不同的controller.有时我们需要在不同的controller中共享数据,本文旨在解决这一问题. 1. 使用$rootScope直接绑定 AngularJS中有一个$rootScope对象,它是AngularJS中最接近全局作用域的对象,是所有$scope对象的最上层,可以简单理解为BOM中的window对象或Node.js中的global对象.最简单的方式是直接将要共享的数据绑定到$rootScope对象中: <!DOCTYPE html>

跟我学AngularJs:Controller数据共享、继承、通信使用详解

林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 摘要:本文主讲了AngularJs中的Controller中数据共享.继承.通信的详细使用 本教程使用AngularJs版本:1.5.3 AngularJs GitHub: https://github.com/angular/angular.js/ AngularJs下载地址:https://angularjs.org/ 一.controller基础与用法 AngularJS中的contr