1

How to initialise value inside controller function with AngularJS tried multiple time but not getting proper answer??

1
  • use $scope service to initialize variables and functions inside controller. I would suggest you to first go through some tutorial for angular js.Here is link to quickstart angular js basics- link Commented Dec 16, 2015 at 6:45

1 Answer 1

3

You can inject $scope in your controller function. Then you can define any variable inside $scope.

Example

.controller('TestController', ['$scope', function($scope) {
  $scope.message = 'Hello';
}])

As you can see in the above example I have injected $scope to TestController and defined message variable inside it.

Note that all the variables defined in a controller scope are accessable by the view belonging to that controller. So you can access the value of $scope.message in your DOM as

<div ng-controller="TestController">
  {{message}}
</div>

On the screen you will see the text Hello

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

Comments

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.