尝试使用angularjs中的ng-repeat填充html选择

时间:2017-01-06 09:06:10

标签: html angularjs select ng-repeat

我正在尝试使用ng-repeat在html中填充我的选择,但它不起作用。我观察到当我检查浏览器时,我的ng-repeat似乎被评论了。

我的HTML:

 <select ng-model="transportcompanies" ng-options="TransportCompany in transportcompanies" value="{{TransportCompany.denumire}}">
    <option>{{ TransportCompany.denumire }}</option>
  </select>

我从server.js获取服务:

app.get('/#!/races/insert', (req, res) => {
  TransportCompany.findAll({
    attributes: ['id_firma', 'denumire']
  })
  .then((transportcompanies) => {
    res.status(200).send(transportcompanies)
  })
    .catch((error) => {
      console.warn(error)
      res.status(500).send('error')
    })
})

我的控制器:

angular.module('raceModule').controller('racesInsertController', ['$scope', '$http', '$state', function($scope, $http, $state) {

const SERVER = 'https://proiectweb-sebastianburchi.c9users.io/#!'



    let $constuctor = () => {
        $http.get(SERVER + '/races/insert')
            .then((response) => {
              $scope.race=response.data
            })
            .catch((error) => console.log(error))
    }

  $constuctor()

  $scope.open = function($event,opened) {
    $event.preventDefault();
    $event.stopPropagation();
    $scope[opened] = true;
  };

   $scope.getTemplate = (TransportCompany) => {

        return 'display'
    }

}])

我编辑了我的帖子试图使用ng-options.Ng-repeat不起作用。现在它显示了我,但我有TransportCompany.denumire作为选项..不是它的值

5 个答案:

答案 0 :(得分:0)

尝试这个

                <option ng-repeat="TransportCompany in    transportcompanies">     {{TransportCompany.denumire}}</option>

答案 1 :(得分:0)

您为ng-repeat迭代器对象和选项编写了相同的名称。因此,当您从选择列表中选择一个选项时,值会超过写入。因此,将名称更改为其他名称:

<select ng-model="transportcompanies">
   <option ng-repeat="TransportCompany in transportcompanies" value="{{TransportCompany.denumire}}">TransportCompany.denumire</option>
</select>

为:

<select ng-model="someothername">
   <option ng-repeat="TransportCompany in transportcompanies" value="{{TransportCompany.denumire}}">TransportCompany.denumire</option>
</select>

答案 2 :(得分:0)

<select>
    <option ng-repeat="option in options" ng-value="option.html">
      {{option.display}}
    </option>
  </select>

请参阅plunker https://plnkr.co/edit/EgOJaWCjQtv7jDB5RS39?p=preview

答案 3 :(得分:0)

首先,请保持命名约定。除此之外,下面应该工作正常:

<select ng-model="transportcompanies">
   <option ng-repeat="TransportCompany in transportcompanies" value="{{TransportCompany.denumire}}">{{TransportCompany.denumire}}</option>
</select>

供参考:Plunkr link

答案 4 :(得分:0)

如果您的回复数据已分配到$scope.race,请尝试重复race

<select ng-model="transportcompanies" ng-options="TransportCompany in race" value="{{TransportCompany.denumire}}">
  <option>{{ TransportCompany.denumire }}</option>
</select>