Wednesday, 1 June 2016

32. Angularjs Controller as syntax

till we used $ccope object to access properties from controller to view. without using $scope object also we can achive this with the help of this keyword and controller reference.

Demo :

 
var mymodule = anjular.module("app", [])
mymodule.controller("Maincontroller", function () {
    this.message="this keyword"
});

now we can access message property by creating reference/object to "Maincontroller" in view like


<body ng-app="app">
    <div ng-controller="Maincontroller as MainCtrl">
        {{MainCtrl.message}}
    </div>
    </body>

as well 
now in referenced templates also need to change syntax as 






{{homeCtrl.message}}

{{studentsCtrl.students}}
{{CourseCtrl.course}}
{{StudentDetailsCtrl.Student}}.

one more importent point to while getting data from services we should use this keyword like below



No comments:

Post a Comment