2016-09-28 114 views
0

我有对象的这样一个数组:打印阵列(角JS)

$scope.sales = [ 
{id: 15, location:'neverland'}, 
{id: 16, location:'farawayland'}, 
{id: 17, location:'highland'} 
]; 

这阵我想在一个表中显示,它应该是这样的:

id |地点


15 | neverland


16 | farrawayland


17 |高地

我的HTML:

<input type="text" ng-model="sales[0].id"> 
<table class="table table-hover"> 
    <tr ng-repeat="x in sales"> 
     <td>{{x.id}}</td> 
     <td>{{x.location}}</td> 
    </tr> 


</table> 

输入字段具有值15被打印。如果我也问第二或第三个值,它会起作用。但该表由3个空行组成。

如果我添加一个索引(即使是<td>{{x[0].id}}</td>),我得到一个服务器错误。

+0

右击并选择 “检查元素”。这将清楚说明内容是否真的存在,但只是没有显示。如果您的CSS不合适,可能会发生这种情况。 –

+0

@Amyblankenship已经做到了。没有内容,只是空的​​ –

+0

你显示的应该可以正常工作。创建一个演示,重现这一点......非常简单,可以将其设置在运动中 – charlietfl

回答

0

它适用于我,请检查它here

也许你错过了别的东西。

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

<head> 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
    <script type="text/javascript" src="script.js"></script> 
</head> 

<body ng-controller="Controller"> 
    <input type="text" ng-model="sales[0].id"> 
    <table class="table table-hover"> 
    <tr ng-repeat="x in sales"> 
     <td>{{x.id}}</td> 
     <td>{{x.location}}</td> 
    </tr> 
    </table> 
</body> 

</html> 

的script.js

var app = angular.module('app', []); 

app.controller("Controller", ['$scope', function($scope) { 

    $scope.sales = [{ 
    id: 15, 
    location: 'neverland' 
    }, { 
    id: 16, 
    location: 'farawayland' 
    }, { 
    id: 17, 
    location: 'highland' 
    }]; 

}]);