Part AngularJS Controller as syntax

Source: Internet
Author: User

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

function ($scope) {    = "Hello Angular";});

In the example above we were attaching message property to $scope, which was automatically available in the view.

<ng-controller= "Maincontroller">{{message}}</  H1>

The another-on-the-available to expose-from the controllers to the view, are by using controller as Sy Ntax. With this syntax, there are no need to inject $scope object in to the controller function, instead you simply use this KEYW Ord as shown below.

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 so we worked with In part 31.

Code changes in script.js. Notice the changes highlighted in yellow.
1. In the IF () function, notice that we are using the CONTROLLER as Syntax
2. With all controller () function we are using this keyword to set the properties of we want to make available in the V Iew
3. Notice in Studentscontroller and Studentdetailscontroller we is assigning this keyword to variable VM. VM stands for ViewModel. You can give it all meaningful name you want.
4. If You use this keyword and then () function as shown below, you would not get the result of you expect. That's because ' this ' keyword points to the Window object when the control comes to then () function. 

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, the HomeController has set.

<H1>{{Homectrl.message}}</H1> <Div>Pragim established in the 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 has more than of 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, the Coursescontroller has set.

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

changes in students.html: Use Studentsctrl object to retrieve the students property, the Studentscontroller has set.

<H1>List of Students</H1><ul>    <Ling-repeat= "Student in studentsctrl.students">        <ahref= "Students/{{student.id}}">{{Student.name}}</a>    </Li></ul>

changes in studentdetails.html: Use Studentdetailsctrl object to retrieve the student property, the Studentdetailscontroller has set.

<H1>Student Details</H1><Tablestyle= "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><ahref= "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.ht                    ML ", 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.htm                     L ",   Controller: "Studentdetailscontroller as Studentdetailsctrl", Controlleras: "Studentdetailsctrl"                }). Otherwise ({redirectto: "/Home"})            $locationProvider. Html5mode (True); })

In our next video we'll discuss, how the CONTROLLER as syntax can make our code more readable as opposed to USI Ng $scope when working with nested scopes.

Part AngularJS Controller as syntax

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.