javascript - AngularJs 不显示从 Controller 填充的列表

标签 javascript angularjs

我正在通过教程学习 Angular video ,但我无法让以下示例工作。该示例展示了如何使用 View1.html 来使用路由,该 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
            }
        );
    };
}
);

最佳答案

在你的 li 元素中你需要放入

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

关于javascript - AngularJs 不显示从 Controller 填充的列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28074750/

相关文章:

javascript - Node 抛出 module.js :340 error

javascript - 获取 Javascript 文件中的 PHP 变量

javascript - 在尝试在 .Net 母版页中使用 jQuery 函数之前加载 jQuery

javascript - 重定向到另一个 HTML 页面在 Javascript 表单提交事件监听器中不起作用

Javascript (html) - 使变量与 .src 的 img 具有相同的值

angularjs - 状态对象的自定义数据

angularjs - 我可以将匿名函数或代码作为指令中的属性吗?

angularjs - 动态添加元素的指令不起作用

javascript - 将 ng-bind 与变量谓词一起使用

javascript - 使用 javascript 构造 HTML 并传递变量(onclick 函数) - 引号/双引号语法问题