I'm currently working on this code:
<select class="form-control"
ng-init="agreementTypes = initAgreementTypes('ManageMission/GetDescriptions/?type=AgreementTypes')"
ng-model="mission.AgreementType"
ng-options="at.Content for at in agreementTypes"></select>
<select class="form-control"
ng-init="agreementStatus = initAgreementStatus('ManageMission/GetDescriptions/?type=AgreementStatus')"
ng-model="mission.AgreementStatus"
ng-options="as.Content for as in agreementStatus"></select>
$scope.initAgreementStatus = function (url) {
$http.get(url).
success(function (data, status, headers, config) {
$scope.agreementStatus = data;
}).
error(function (data, status, headers, config) {
console.log("data: " + data);
console.log("status: " + status);
console.log("headers: " + headers);
console.log("config: " + config);
});
};
$scope.initAgreementTypes = function (url) {
$http.get(url).
success(function (data, status, headers, config) {
$scope.agreementTypes = data;
}).
error(function (data, status, headers, config) {
console.log("data: " + data);
console.log("status: " + status);
console.log("headers: " + headers);
console.log("config: " + config);
});
};
This code works, but it's quite ugly. I would like to merge the two functions but I don't know the best way to do it. I've already read some articles about promise. Please keep in mind that I would like to keep URLs in the HTML part.
You can access your $scope
attributes like on any other Object
using brackets:
$scope['agreementTypes']
This way, you can add the name of the attribute, the response should be assigned to:
$scope.initDescriptions = function (target, url) {
$http.get(url)
.success(function (data, status, headers, config) {
$scope[target] = data;
})
.error(function (data, status, headers, config) {
console.log("data: " + data);
console.log("status: " + status);
console.log("headers: " + headers);
console.log("config: " + config);
});
};
In your markup, apply the following changes:
ng-init="initDescriptions('agreementTypes', 'ManageMission/GetDescriptions/?type=AgreementTypes')"