Angularjs 使用角度js中的数组设置模型值
我正在使用AngularJS框架 我有一个文本框来输入金额,还有一个下拉框来选择类型Angularjs 使用角度js中的数组设置模型值,angularjs,Angularjs,我正在使用AngularJS框架 我有一个文本框来输入金额,还有一个下拉框来选择类型 <input type="text" ng-model="user.amount"/> <select ng-model="user.rateType"> <option> rate </option> <option> unrate</option> </select> <input type="text" n
<input type="text" ng-model="user.amount"/>
<select ng-model="user.rateType">
<option> rate </option>
<option> unrate</option>
</select>
<input type="text" ng-model="user.assignVal"/>
用户在文本框中输入金额并选择费率类型。
我需要找到满足以下条件的元素:
- 用户选择的类型与项目
type
- 用户输入的金额在由
和hval
lval
- 用户输入1100作为金额,并选择
type,rate
等于20cal
- 用户输入6500作为金额,并选择
type,rate
等于50cal
- 用户输入1100作为金额,然后选择
type,unrate
等于100cal
$scope.loadShareSetting = function (){
$http({
method: "GET",
headers: { 'Content-Type': 'application/json','Authorization': 'Bearer ' + localStorage.getItem('JWT_TOKEN')},
url: appConfig.apiUrl + "/residence-settings",
}).then(function (response) {
$scope.residenceSetting = response.data;
}, function (response) {
});
}
下面是实现这一点的示例代码
angular.module(“myApp”,[]);
有棱角的
.模块(“myApp”)
.controller(“myController”[
“$scope”,
“我的服务”,
功能($scope,myService){
$scope.getCalForAmount=函数(){
var result=myService.getCalForAmount($scope.amount,$scope.type);
如果(结果!=-1){
$scope.calForAmount=result.cal;
}否则{
$scope.calForAmount=”“;//没有匹配的cal,字段为空
}
};
},
])
.service(“myService”,函数(){
可变项目=[
{id:“1”,hval:1000,lval:5000,类型:“速率”,cal:“20”},
{id:“2”,hval:6000,lval:10000,类型:“速率”,cal:“50”},
{id:“3”,hval:1000,lval:5000,类型:“unrate”,cal:“100”},
{id:“4”,hval:6000,lval:10000,类型:“unrate”,cal:“100”},
];
返回{
getCalForAmount:函数(金额、类型){
var result=items.find(函数(项){
返回(
/^[0-9]*$//.test(amount)&&&//amount必须是一个数字
类型===item.type&&//type必须匹配
amount>=item.hval&&//amount在hval分隔的范围内
谢谢你,Stephane。我会把这个用到我的实际代码中。看起来还可以:)下面的代码会发生什么,返回结果| |-1;?亲爱的朋友,我的实际数据来自get请求。“var items”数组应该从get请求数据中替换。我是如何做的。我在问题中添加了get请求。如果没有项匹配val范围和类型组合,请检查结果以负数返回。在这种情况下,我们不做任何操作我在Plunker上添加了另一个演示,以了解如何使用http请求加载项。这个get请求响应与您的数组非常相似。我如何分配它
$scope.loadShareSetting = function (){
$http({
method: "GET",
headers: { 'Content-Type': 'application/json','Authorization': 'Bearer ' + localStorage.getItem('JWT_TOKEN')},
url: appConfig.apiUrl + "/residence-settings",
}).then(function (response) {
$scope.residenceSetting = response.data;
}, function (response) {
});
}