2

how to set value of ng-model with dot from controller?

<input type=text" ng-model="user.latitude">

this doesn't work:

$scope.user.latitude = myLat;
1
  • use $scope.user = {latitude: myLat}; in your controller Commented Jun 4, 2014 at 15:24

3 Answers 3

4

You need to create user object first:

$scope.user = {};
$scope.user.latitude = myLat;

or shorter:

$scope.user = {latitude: myLat};
Sign up to request clarification or add additional context in comments.

1 Comment

yes, but this will clear other values of user, for example 'ng-model="user.name"', how to edit only 'user.latitude'?
0

I have users_review as array of objects and just to replace specific key of array element, I have modified my code as,

$scope.users_review[index].selected = true;

Comments

0

This is the only possible way of doing this. You have to make an object first then only you can enter something into it.

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="name.fname"><br>



</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
	$scope.name={};
   $scope.name.fname="test";
   
   //$scope.name={fname:"test"};
});
</script>

</body>
</html>

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.