Ng-option不起作用

时间:2016-03-17 05:05:31

标签: javascript angularjs

正如标题所说,我的ng-option有问题,这是代码

<div class="col col-10">
    <label class="select disabled">
        <select data-ng-option="h.id as h.time for h in dataDiri.home_service_time"
                name="gender"
                ng-model="jamlayanan">
        </select>
    </label>
</div>

这是我的angularjs代码中的http get函数:

$scope.lihatDataDiri = function(){
    $http.get('/someurl').success(function(data){
        $scope.dataDiri = data;
    });
};

结果是我的选择不起作用,这是我想要的json

"home_service_time": [{
        "id": "1",
        "time": "08:00 WIB - 10:00 WIB"
    }, {
        "id": "2",
        "time": "10:00 WIB - 12:00 WIB"
    }, {
        "id": "3",
        "time": "12:00 WIB - 14:00 WIB"
    }, {
        "id": "4",
        "time": "14:00 WIB - 16:00 WIB"
    }],

所以在这种情况下,我想在下拉列表中选择我的时间,我将传递的价值是“id”,任何人都可以帮助我吗?我通过谷歌搜索过,但这是我的最新作品。

2 个答案:

答案 0 :(得分:0)

通过观察Json你应该试试这个

     <select data-ng-options="h.id as h.time for h in dataDiri" name="gender" ng-model="jamlayanan">

        </select>

答案 1 :(得分:0)

以下是data-ng-options的{​​strong> PLUNKER :http://plnkr.co/edit/c3X5bqEcrmKQXXuud4hW?p=preview

考虑到您使用json提供的格式获取数据。

<!DOCTYPE html>
<html>

  <head>
    <script data-require="angular.js@1.4.9" data-semver="1.4.9" src="https://code.angularjs.org/1.4.9/angular.js"></script>
    <link rel="stylesheet" href="style.css" />
  </head>

  <body ng-app="app">
    <div ng-controller="myCtrl">
         <select data-ng-options="h.id as h.time for h in dataDiri.home_service_time" ng-model="jamlayanan ">
               <option value="" disabled>Select</option>
         </select>
      <br>
      <br>
      Data: <input type="text" ng-model="jamlayanan">
    </div>
    <script>
    var app = angular.module('app', []);

    app.controller('myCtrl',function($scope){
      $scope.dataDiri ={ "home_service_time": [{
        "id": "1",
        "time": "08:00 WIB - 10:00 WIB"
    }, {
        "id": "2",
        "time": "10:00 WIB - 12:00 WIB"
    }, {
        "id": "3",
        "time": "12:00 WIB - 14:00 WIB"
    }, {
        "id": "4",
        "time": "14:00 WIB - 16:00 WIB"
    }] };
    });
    </script>
  </body>

</html>