NG-选择与目标数据源 [英] ng-options with object data source

查看:130
本文介绍了NG-选择与目标数据源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我挣扎理解NG选项是如何工作的一个数据源。我读过的文档,我觉得我做的是需要什么,但是当试图我仍然获得问题。

I'm struggling to understand how ng-options works with a data source. I've read the docs and I feel like I'm doing exactly what is required, but I still get problems when attempting.

<!DOCTYPE html>
<html ng-app="app">

<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.1.5/angular.min.js">              </script>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript">
  angular.module('app', [])
    .controller('IndexCtrl', ['$scope', function($scope) {
      $scope.types = {
        1: "value1",
        2: "value2",
        5: "value3"
      };
    }]);
</script>
</head>

<body ng-controller="IndexCtrl">
<select ng-model="type" ng-options="k as v for(k, v) in types">
  <option value="">Select Type</option>
</select>
</body>

</html>

我总是得到这个错误在控制台:

I always get this error in the console:

错误:预期ngOptions在'形式的选择的(如的标签的)?对(的,)?的的中的的(按曲目的 EXPR 的)?但得到数k V代表(K,V)的类型。

Error: Expected ngOptions in form of 'select (as label)? for (key,)?value in collection (track by expr)?' but got 'k as v for(k, v) in types'.

我在做什么错误?

请参阅plunkr这里:

See plunkr here:

<一个href=\"http://plnkr.co/edit/Bl6u4151KyDkxhYrsdCm?p=$p$pview\">http://plnkr.co/edit/Bl6u4151KyDkxhYrsdCm?p=$p$pview

推荐答案

这是一种奇怪的,但似乎你需要把一个空格后。这工作:

This is kind of strange, but it seems like you need to put a space after for. This works:

<select ng-model="type" ng-options="k as v for (k, v) in types">
  <option value="">Select Type</option>
</select>

这篇关于NG-选择与目标数据源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆