javascript - declare multiple variables into a variable angularjs -
how put multiple variables 1 variable can minify code usage. how declare can use $scope.bundle instead of these 3 variables : $scope.acc.city, $scope.acc.state , $scope.acc.country. global variable carry multiple variables can use everywhere without need write variables again , again.
the way want :
$scope.bundle = [{$scope.acc.city, $scope.acc.state, $scope.acc.country}]; $scope.postcode = resp.content; if ($scope.postcode.length > 0) { $scope.bundle = $scope.postcode[0]; $scope.acc = $scope.bundle; } else { $scope.bundle = ''; $scope.acc = $scope.bundle; }
instead of :
if ($scope.postcode.length > 0) { var data = $scope.postcode[0]; $scope.acc.city = data.city; $scope.acc.state = data.state; $scope.acc.country = data.country; } else { $scope.acc.city = ''; $scope.acc.state = ''; $scope.acc.country = ''; }
you're creating array of objects, need provide keys
values
$scope.bundle = [ { city : $scope.acc.city, state : $scope.acc.state, country : $scope.acc.country } ];
then can access like
$scope.bundle[0].city
if you've got 1 item (instead of array) can create object directly
$scope.bundle = { city : $scope.acc.city, state : $scope.acc.state, country : $scope.acc.country };
then can access without index
property
$scope.bundle.city
Comments
Post a Comment