如何用通用函数 (AngularJS) 填充多个 select?
How to populate several select with a generic function (AngularJS)?
我目前正在处理这段代码:
<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);
});
};
此代码有效,但它非常难看。我想合并这两个功能,但我不知道最好的方法。我已经阅读了一些关于 promise 的文章。请记住,我想将 URL 保留在 HTML 部分。
您可以像访问任何其他 Object
一样使用括号访问您的 $scope
属性:
$scope['agreementTypes']
这样,你可以添加属性的名称,响应应该分配给:
$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);
});
};
在您的标记中,应用以下更改:
ng-init="initDescriptions('agreementTypes', 'ManageMission/GetDescriptions/?type=AgreementTypes')"
我目前正在处理这段代码:
<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);
});
};
此代码有效,但它非常难看。我想合并这两个功能,但我不知道最好的方法。我已经阅读了一些关于 promise 的文章。请记住,我想将 URL 保留在 HTML 部分。
您可以像访问任何其他 Object
一样使用括号访问您的 $scope
属性:
$scope['agreementTypes']
这样,你可以添加属性的名称,响应应该分配给:
$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);
});
};
在您的标记中,应用以下更改:
ng-init="initDescriptions('agreementTypes', 'ManageMission/GetDescriptions/?type=AgreementTypes')"