角度js中的ng-options问题

时间:2017-05-31 15:07:56

标签: angularjs html5

我是角度js的新手,并试图添加选项以使用角度但面临问题:

以下是代码

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var mymodule = angular.module("firstform",['$scope']);


mymodule.controller("selectgroupcontroller",['$scope', function($scope){
        
       // $scope.optgrps = selectGroupFactory.optgrps;
        $scope.optgrps = [
                                {name : 'First', value : '1', group : '1-3'},
                              {name : 'Second', value : '2', group : '1-3'},
                              {name : 'Third', value : '3', group : '1-3'},
                              {name : 'Fourth', value : '4', group : '4-6'},
                              {name : 'Fifth', value : '5', group : '4-6'},
                              {name : 'Sixth', value : '6', group : '4-6'},
                              
                              
                             ] ;
}]); 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="row" ng-app="firstform">
  <div class="col-sm-12">
    <form name="selecttest" novalidate="" ng-controller="selectgroupcontroller">
      <div class="form-group">
        <div class="control-label text-center col-sm-2">
          <label for="selectgrp">Select Value: </label>
        </div>
        <div class="col-sm-10">
          <select class="form-control" ng-model="selectedVal" id="selectgrp" ng-options="val.value as (val.name+val.value) group by val.group for val in optgrps">

          </select>
        </div>
      </div>
    </form>


  </div>
</div>

为选择选项和打印添加组很简单。 但它没有给出任何错误,也没有给出任何输出。

1 个答案:

答案 0 :(得分:1)

它提供错误Error: [$injector:modulerr]删除$ scope module inject

var mymodule = angular.module("firstform",[]);

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var mymodule = angular.module("firstform",[]);


mymodule.controller("selectgroupcontroller",['$scope', function($scope){
        
       // $scope.optgrps = selectGroupFactory.optgrps;
        $scope.optgrps = [
                                {name : 'First', value : '1', group : '1-3'},
                              {name : 'Second', value : '2', group : '1-3'},
                              {name : 'Third', value : '3', group : '1-3'},
                              {name : 'Fourth', value : '4', group : '4-6'},
                              {name : 'Fifth', value : '5', group : '4-6'},
                              {name : 'Sixth', value : '6', group : '4-6'},
                              
                              
                             ] ;
}]); 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="row" ng-app="firstform">
  <div class="col-sm-12">
    <form name="selecttest" novalidate="" ng-controller="selectgroupcontroller">
      <div class="form-group">
        <div class="control-label text-center col-sm-2">
          <label for="selectgrp">Select Value: </label>
        </div>
        <div class="col-sm-10">
          <select class="form-control" ng-model="selectedVal" id="selectgrp" ng-options="val.value as (val.name+val.value) group by val.group for val in optgrps">

          </select>
        </div>
      </div>
    </form>


  </div>
</div>