如何在angularjs中的选择框或内部选项中使用ng-model?

时间:2018-03-05 08:12:02

标签: angularjs angularjs-scope

我有一个包含选择框的表格数据。我想使用ng-model而不是{{n.triggerOn}来绑定数据。 当我使用log时,以下代码正常工作。但我想使用ng -model <option value="">{{n.triggerOn}}</option>

绑定数据
<option ng-model="n.triggerOn"></option>

我以JSON格式获取数据

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

任何人都可以帮助我吗?我是angularjs的新手。对不起,如果这是一个愚蠢的错误。三江源。

3 个答案:

答案 0 :(得分:1)

在select标签中使用ngmodel属性,在选项而不是值使用ng-value中,告诉我它是否无效。

答案 1 :(得分:1)

这是工作代码

如果您需要更改,请与我们联系。

<!DOCTYPE html>
<html>

<head>
  <script data-require="angular.js@1.6.6" 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>

答案 2 :(得分:0)

您需要在布尔值中再发送一个名为selected的变量。

 $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 }
    ];

然后在html

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