Search code examples
angularjshidden-field

Angular JS hidden field : saving ng-model with condition


I have a hidden field, which is used to identify the category of the directive.

How can I bind the data as :

<input type = "hidden" ng-model="category = category.option.name == 'By Count Range' ? 'byCount' : 'byRate' ">

The above code is not assignable while compiling.

How can I solve this?

I'm not sure about my code, but I need to perform that condition check.


Solution

  • So, you have with two options: By count range or by rate and depending on which one is selected you want to change the . right?

    Then, you can do this in template:

    <select name='...' ng-model='...' ng-change='updateOptions()'>
    

    and in your controller:

    $scope.updateOptions = function () {
        if ($scope.category.priceOrTransactionOption == 'By Count Range'){
          $scope.categoryOptions = $scope.category.byCountRange.tiers
        }else{
          $scope.categoryOptions = $scope.category.byRate.tiers;
        }
    };
    

    and in html:

    <div ng-repeat="categoryObject in categoryOptions track by $index">
    
    <div>