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>
https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js
<body>



First Name:






var app = angular.module('myApp', );
app.controller('myCtrl', function($scope) {
$scope.name={};
$scope.name.fname="test";

//$scope.name={fname:"test"};
});


</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

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages