如何更改ng-model?

时间:2015-04-24 10:54:11

标签: angularjs

有Angular JS:

$scope.changeSelect = function (item){
    if(item == 0){
      // Здесь изменить ng-model элемента
    }
}

HTML:

<select ng-model="city" ng-change="changeSelect(city)">
<select ng-model="country" ng-change="changeSelect(country)">

如果条件为真,我需要为每个ng-model重置ng-change

我想要什么

当我调用函数ng-change="changeSelect()"时,我需要检查零值,如果为真,则将此ng-model值更改为null。更新了问题

3 个答案:

答案 0 :(得分:0)

试试这个

Html:

<select ng-model="city" ng-change="changeSelect(this.city)">
<select ng-model="country" ng-change="changeSelect(this.country)">

JS Cde

$scope.changeSelect = function (item){
    if (item == 0) {
         item=null;
    }
}

更新:

另一种方法是手动思考方式:p

 <select ng-model="city" ng-change="changeSelect(this.city,'city')">
    <select ng-model="country" ng-change="changeSelect(this.country,'country')">

.........

$scope.changeSelect = function (item, selectedContex) {
            if (item == 0) {
                if (selectedContex == "city") {
                    $scope.city = null;
                    return false;
                }
                else if (selectedContex = 'country') {
                    $scope.country = null;
                    return false;
                }
            }
        }

答案 1 :(得分:0)

HTML:

<select  ng-model="(changeSelect(city) ? null: city)">

脚本:

$scope.changeSelect = function (item){
    if(item == 0){
      return true;
    }
    else {
      return false;
    }
}

答案 2 :(得分:0)

您可以使用我撰写的angular-reset-on指令。使用此指令,您可以指定重置每个字段的条件。

例如:

<select ng-model="city" ng-reset-on="city == 0">
<select ng-model="country" ng-reset-on="country == 0">

就这么简单。 :)