Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Assign value with ng-change in angular js

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?

like image 734
Shivkumar Avatar asked Sep 30 '13 08:09

Shivkumar


People also ask

How do you use NG value?

The AngularJS ng-value directive is used to set the value attribute of an input element, or a select element. It is mainly used on <radio> and <option> elements to set the bound values when these elements are selected. It is supported by <input> and <select> elements.

What is $Watch in AngularJS?

$watch() function is used to watch the changes of variables in $scope object. Generally the $watch() function will create internally in Angularjs to handle variable changes in application.

How do you use NG bind?

Definition and UsageThe ng-bind directive tells AngularJS to replace the content of an HTML element with the value of a given variable, or expression. If the value of the given variable, or expression, changes, the content of the specified HTML element will be changed as well.

What is ngInit?

The ngInit directive allows you to evaluate an expression in the current scope. This directive can be abused to add unnecessary amounts of logic into your templates. There are only a few appropriate uses of ngInit : aliasing special properties of ngRepeat , as seen in the demo below.


1 Answers

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.....

like image 59
Maxim Shoustin Avatar answered Nov 11 '22 19:11

Maxim Shoustin