Angularjs: ng-model setting value from controller


Angularjs: ng-model setting value from controller



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;





use $scope.user = {latitude: myLat}; in your controller
– guru
Jun 4 '14 at 15:24




3 Answers
3



You need to create user object first:


user


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



or shorter:


$scope.user = {latitude: myLat};





yes, but this will clear other values of user, for example 'ng-model="user.name"', how to edit only 'user.latitude'?
– user1824542
Jun 4 '14 at 15:35



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;



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>






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy