How to Assign value with ng-change in angular js

五迷三道 提交于 2019-12-04 09:02:41

问题


I have simple drop-down bind with angular model

<select ui-select2="{allowClear:true}" ng-model="product.Id" ng-change="{value = product.Id == 0}" data-placeholder="Select Warranty">
      <option></option>
      <option ng-repeat="product in products" value="{{product.Id}}">{{product.Code}}</option>
</select>

How i can assign value depending on some condition in ng-change?


回答1:


Your selected value is defined as ng-model. On ng-change you can call a method from the controller and provide the "selected" ng-model to this method.

Here is an example:

<select                                               
        ng-model="product.Id"
        ng-options="filter as filter.name for filter in groupList"
        ng-change="changeItem(product.Id)"
         ></select>

Controller

$scope.changeItem = function(iem){

}

As a side note, I would use ng-options instead of <option ng-repeat.....




回答2:


Add a function to the scope that checks the condition and assigns the value if it is true. Simply call this function from ng-change.



来源:https://stackoverflow.com/questions/19089411/how-to-assign-value-with-ng-change-in-angular-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!