Ahmed
Ahmed

Reputation: 255

How get access to variable in Angular JS?

I have next Angular JS controllers structure:

<div ng-controller="MainController">
   <div ng-controller="mainCtrlTst">
      // here is loaded HTML file  
   </div>
</div>

HTML file:

<div ng-controller="MainController">
  <input ng-model="formData.map" value="">
</div>

For default formData.map containts address "USA, New York" I have method Save() in MainController, but when I call this method I get:

console.log(formData.map); // undefined

How I can get value formData.map from input?

Upvotes: 1

Views: 45

Answers (2)

Hasib Tarafder
Hasib Tarafder

Reputation: 5813

You should declare your model in controller like

$scope.formData = {
    map: ''
};

And then use it in the view.

And then check in the save method by following code

console.log($scope.formData.map);

Hope you will not get undefined.

Upvotes: 1

Roy M J
Roy M J

Reputation: 6948

Try the following in MainController:

console.log($scope.formData.map);

To be more precise, in your save() function inside the controller do as follows:

$scope.save = function(){
    console.log($scope.formData.map);
}

To know more about $scope, visit : https://docs.angularjs.org/guide/scope

Upvotes: 0

Related Questions