Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用角度js中的数组设置模型值_Angularjs - Fatal编程技术网

Angularjs 使用角度js中的数组设置模型值

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

我正在使用AngularJS框架

我有一个文本框来输入金额,还有一个下拉框来选择类型

<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作为金额,并选择
    rate
    type,
    cal
    等于20
  • 用户输入6500作为金额,并选择
    rate
    type,
    cal
    等于50
  • 用户输入1100作为金额,然后选择
    unrate
    type,
    cal
    等于100
我怎样才能做到这一点

获取请求

$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) {

    });

}