Sai Manoj
Sai Manoj

Reputation: 3849

How to use ng-model inside select box or inside options in angularjs?

I'm having a table data which consists of a select box. I want to bind the data using ng-model instead of {{n.triggerOn}. The below code is working properly when I use<option value="">{{n.triggerOn}}</option>. But I want to bind the data using ng -model <option ng-model="n.triggerOn"></option>

                <td>
                    <select class="browser-default event trigger">
                        <option value="">{{n.triggerOn}}</option>
                        <option value="all">
                            All Conditions
                        </option>
                        <option value="any">
                            Any Conditions
                        </option>
                    </select>
                </td>

I'm getting the data in JSON format as

             rule: {
                        name:"abc",
                        status:"enable",
                        triggerOn: "all",
                        onSuccess: "on"
                    }

Can anyone help me? I'm new to angularjs. Sorry if it was the silly mistake. Thankyou.

Upvotes: 1

Views: 192

Answers (3)

KishoreKumar Mani
KishoreKumar Mani

Reputation: 36

You need to send one more variable called selected in boolean.

 $scope.rules = [
      {name:"abc", status:"enable", triggerOn: "none", onSuccess: "on", selected: false },
      {name:"abc", status:"enable", triggerOn: "any", onSuccess: "on", selected: true},
      {name:"abc", status:"enable", triggerOn: "all", onSuccess: "on", selected: false }
    ];

then bind ng-selected attr in html

<option ng-repeat="rule in rules" value="{{rule.triggerOn}}"
                    ng-selected="{{ rule.selected == true }}">

Upvotes: 0

Mohamed Wahshey
Mohamed Wahshey

Reputation: 422

Use ngmodel attribute in the select tag, in the option instead of value use ng-value, tell me if it didnt work.

Upvotes: 1

Nishanth
Nishanth

Reputation: 835

here is the working code

Let me know if you need any changes in that.

<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="1.6.6" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.6/angular.min.js"></script>
  <script src="https://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.6.0.js" type="text/javascript"></script>
  <script>
    (function() {

      angular.module("myApp", []).controller('test', ['$scope', function($scope) {

        $scope.rules = [
          {name:"abc", status:"enable", triggerOn: "none", onSuccess: "on" },
          {name:"abc", status:"enable", triggerOn: "any", onSuccess: "on" },
          {name:"abc", status:"enable", triggerOn: "all", onSuccess: "on" }
        ];

      }]);


    }());
  </script>
  <style></style>
</head>

<body ng-app="myApp">
  <div ng-controller="test">
    
    <select class="browser-default event trigger" ng-model="selectedRule" 
    ng-options="rule.triggerOn for rule in rules track by rule.triggerOn"></select>

    <div>
      <b>Selected Rule:</b> {{selectedRule.triggerOn}}
    </div>

  </div>
</body>

</html>

Upvotes: 1

Related Questions