Я назначил локальные данные о состоянии страны в $scope.countries
и передал их на бэкэнд, теперь в $ scope.getValue() Я получу значения для города страны страны из бэкэнд. Но я не удалось присвоить значения, которые я получаю от backend-ответа к моей ng-модели $scope.countrySrc
по умолчанию.Назначение значения для раскрывающегося списка по умолчанию
<!DOCTYPE html>
<html data-ng-app="myApp">
<head>
<link rel="stylesheet" href="style.css">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script>
<script src="script.js"></script>
</head>
<body data-ng-controller="testController">
<label for="country">Country *</label>
<select id="country" ng-model="countrySrc" ng-options="country for (country, states) in countries" ng-change="GetSelectedCountry()">
<option value=''>Select</option>
</select>
<label for="state">State *</label>
<select id="state" ng-disabled="!countrySrc" ng-model="stateSrc" ng-options="state for (state,city) in countrySrc" ng-change="GetSelectedState()">
<option value=''>Select</option>
</select>
<label for="city">City *</label>
<select id="city" ng-disabled="!countrySrc || !stateSrc" ng-model="city" ng-options="city for city in stateSrc">
<option value=''>Select</option>
</select>
<script>
angular
.module('myApp', [])
.run(function($rootScope) {
$rootScope.title = 'myTest Page';
})
.controller('testController', ['$scope',
function($scope) {
$scope.countries = {
'USA': {
'Alabama': ['Montgomery', 'Birmingham'],
'California': ['Sacramento', 'Fremont'],
'Illinois': ['Springfield', 'Chicago']
},
'India': {
'Maharashtra': ['Pune', 'Mumbai', 'Nagpur', 'Akola'],
'Madhya Pradesh': ['Indore', 'Bhopal', 'Jabalpur'],
'Rajasthan': ['Jaipur', 'Ajmer', 'Jodhpur']
},
'Australia': {
'New South Wales': ['Sydney'],
'Victoria': ['Melbourne']
}
};
$scope.GetSelectedCountry = function() {
$scope.strCountry = $scope.countrySrc;
};
$scope.GetSelectedState = function() {
$scope.strState = $scope.stateSrc;
};
UserService.getProfile.then(function (response) {
$scope.strCountry = response.json.response.profile.country;
};
$scope.getProfile();
])
</script>
</body>
</html>
Userservice.jS:
function getProfile(profile){
return $http.post(url+'?request=' + JSON.stringify(profile)).then(handleSuccess, handleError('Error in saving profile details'));
}
отклик от бэкэнда:
{"response":{"json":{"session_id":"498","profile":
{"country":"Afghanistan",
"state":"Badakhshan",
"city":"Eshkashem",
"pincode":"54564","rolemandatory":1},"roles":[]}}}
где ваш ави звонок? что такое «ответ» в вашем вопросе? – Sravan
, где вы назвали эту услугу? – Sravan
Я вызываю услугу отдельно в userervice.js –