0
<h1 ng-controller="mainController as mainCtrl">{{ms}}</h1>

.controller("mainController",function(){
this.ms = "hi";
});

Why are people using as in angularjs? What I usually do is just inject $scope and do $scope.ms = 'something'

0

1 Answer 1

2

You don't need to inject $scope if you do this

<h1 ng-controller="mainController as mainCtrl">{{mainCtrl.ms}}</h1>

app.controller("mainController", function() {
    this.ms = "hi";
});

Better explained here.

EDIT: It's mentioned in AngularJS Official Docs as well. See ngController.

Sign up to request clarification or add additional context in comments.

1 Comment

is this a new thing? if it's a preferred way why don't angular official site don't use it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.