AngularJs不显示从控制器填充的列表

时间:2015-01-21 18:59:01

标签: javascript angularjs

我正在通过编写教程video来学习Angular,但我无法得到以下示例。该示例显示了如何使用View1.html来使用路径,该路径应显示客户列表(简单名称,城市对象)以及添加,过滤客户的可能性。

当我尝试样品时,客户列表不会显示 - 我只能看到子弹点和所有标签,文本等,但没有名称列表 - 城市。 当我使用硬编码列表替换View1.html中的列表时,它会正确显示。

这里有什么问题?

代码

的index.html

<!DOCTYPE html>
<html data-ng-app="demoApp">
<head>
<script  src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular-route.js"></script>
<script src="angularTest.js"></script>
</head>
<body>

<div>
    <div ng-view></div>
</div>



</body>
</html>

View1.html

<div class="container">
<h2>View 1</h2>
Name:
<br/>
<input type="text" data-ng-model="filter.name"/>
<br/>
<ul>
    <li data-ng-repeat="cust in customers | filter:filter.name | orderBy:city"></li>
</ul>
<br/>
Customer name<br/>
<input type="text" data-ng-model="newCustomer.name"/>
<br/>
Customer city<br/>
<input type="text" data-ng-model="newCustomer.city"/>
<br/>
<button data-ng-click="addCustomer()">Add customer</button>

<br/>

<a href="#/view2">View 2</a>

View2.html

<div class="container">
<h2>View 2</h2>
Name:
<br/>
<input type="text" data-ng-model="city"/>
<br/>
<ul>
    <li data-ng-repeat="cust in customers | filter:city | orderBy:city"></li>
</ul>

angularTest.js

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


app.config(function ($routeProvider) {
$routeProvider.when('/',
    {
        controller: 'SimpleController',
        templateUrl: 'partials/View1.html'
    })
    .when('/view2',
    {
        controller: 'SimpleController',
        templateUrl: 'partials/View2.html'
    })
    .otherwise({redirectTo: '/'});
});

app.controller('SimpleController',
function SimpleController($scope) {
    $scope.customers = [
        {name: 'John Doe', city: 'New York'},
        {name: 'Jane Doe', city: 'San Fransisco'},
        {name: 'Joe Smith', city: 'Washington'}
    ];

    $scope.addCustomer = function() {
        $scope.customers.push(
            {
                name: $scope.newCustomer.name,
                city: $scope.newCustomer.city
            }
        );
    };
}
);

2 个答案:

答案 0 :(得分:1)

在你的li元素中你需要放

<li data-ng-repeat="cust in customers | filter:filter.name | orderBy:city">{{cust.name}} ({{cust.city}})</li>

答案 1 :(得分:1)

您应该在li标签内打印一些内容,以便在{{}}上显示某些内容,为您提供以下权限: -

<li data-ng-repeat="cust in customers | filter:city | orderBy:city">{{cust.name}} ({{cust.city}})</li>