Angularjs对象中的if语句取决于HTML选择选项

时间:2017-10-05 12:38:23

标签: angularjs html5

我想将if条件置于对象中以捕获某个2 HTML选项,并且angular将根据用户的选择将一定值发布到数据库

HTML

<tr>
                <td>End Of Contract By</td>
                <td>
                    <select>
                        <option ng-model="Age">Age</option>
                        <option ng-model="NYears">Number Of Years in Service</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Number</td>
                <td>
                    <input type="text" pattern="^[0-9]+$" title="Please enter numbers only" size="10" maxlength="2" ng-model="selectedMember.Num_EndWork">
                    <select>
                        <option ng-model="Months">Months</option>
                        <option ng-model="Years">Years</option>
                    </select>
                </td>
            </tr>

控制器

var Contract = { Code: $scope.selectedMember.Code.Staff_Type_Code, Latin: $scope.selectedMember.Latin.L_Desc, Local: $scope.selectedMember.Local.A_Desc, Nend: $scope.selectedMember.Num_EndWork, Tend: $scope.Type_EndWork, hd: $scope.selectedMember.Hours_Day, dw: $scope.selectedMember.Days_Week }

    if (!$scope.selectedMember.sys_key ) {
        crudService.post(Contract).then(function (response) {
            $scope.Contract = (res.data)
            $scope.selectedMember = {};
            loadrecords();
        }, function (err) {
            console.log("Err" + err);
        });
    }
}

我想如果用户在select选项中选择1st选项而在second second选项中选择1st,则插入到数据库值1

提前致谢

2 个答案:

答案 0 :(得分:1)

选项不应该有ng-model,只有select应该有

 <select ng-model="selectedContract">
   <option>Age</option>
   <option>Number Of Years in Service</option>
 </select>

 <select ng-model="selectedNumber">
   <option value="Months">Months</option>
   <option value="Years">Years</option>
 </select>

然后在控制器内部,只要条件匹配

,就可以检查并插入1
if($scope.selectedContract =='Age' && $scope.selectedNumber =='Months'){

}

答案 1 :(得分:0)

首先更改您的代码。如下。请参阅此链接,了解如何使用选择标记选项。

https://docs.angularjs.org/api/ng/directive/ngOptions https://docs.angularjs.org/api/ng/directive/select https://docs.angularjs.org/api/ng/directive/ngSelected

  <script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.2/knockout-min.js"></script>
  <input data-bind="value: counterofferPremium, valueUpdate: 'afterkeydown', event: {input: findPlace}" type="text" />