1

我想知道是否有人可以快速浏览我的代码并让我知道我是否遗漏了什么。我正在尝试学习 Angular.js,并且在 60 分钟的视频中一直在使用著名的Angular.js。然而,我遇到的问题是 angular 从那以后已经更新,事情有点不同。结果,我无法正确编排我的路线。非常感谢任何人提前。

我真正想知道的是,任何人都可以告诉我为了使这些路线正常工作而我缺少什么吗?

下面是我的 html 中的脚本。我跟着这本书,结果它们是我的 index.html 页面中的脚本标签。

var demoApp = angular.module('demoApp', ['helperModule']);
    var controllers = {};

    demoApp.controller("CustomersController",  function ($scope){
        $scope.customers = [
                {name: 'Dave Jones', city: 'pheonix'},
                {name: 'Dave Jones', city: 'New york'},
                {name: 'Jones suman', city: 'pheonix'},
                {name: 'Naresh babu', city: 'Hyderabad'}
            ];
        });
    var demoApp = angular.module('demoApp', ['ngRoute']);
        demoApp.config(function($routeProvider){
            $routeProvider
                .when('/',
                    {
                        controller: "SimpleController",
                        templateUrl: "views/view1.html"
                    })
                .when('partial2',
                    {
                        controller: "SimpleController"
                        templateUrl: "views/view2.html"
                    })
                .otherwise({ redirectTo: "/"})
        });

        $scope.addCustomer = function(){
            $scope.customers.push({name: $scope.newCustomer.name, city: $scope.newCustomer.city});
        }
<script src = "angular-route.js"></script>

这分别是我的视图#1 和视图#2。我觉得一切都是正确的,但是我无法让任何客户的名字出现。

<div cass = "container">
<h2>View 1</h2>
    Name: 
    <input type = "text" data-ng-model="filter.name" />
    <ul><li data-ng-repeat="cust in customers | filter:filter.name"></ul>
    Customer Name: <br />
    <input type= "text" data-ng-model="newCustomer.name" />
    Customer City: <br />
    <input type= "text" data-ng-model="newCustomer.city" />
    <button data-ng-click="addCustomer()"> Add Customer </button>   
    <a href="#/view2"> View 2</a>   
</div>



<div cass = "container">
<h2>View </h2>
<div ng-controller="CustomersController">
    Search: <input type = "text" ng-model="searchText" />
    {{ searchText }}
    <br />
    <h3> Customers </h3>
    <table>
        <tr ng-repeat="cust in customers | filter:city">
                <td>{{ cust.name }}</td>
                <td>{{ cust.city }}</td>
                <td>{{ cust.total | currency}} </td>
            </tr>
    </table>
</div>
4

1 回答 1

4

您正在覆盖出现问题的应用程序

从那时开始var demoApp = angular.module('demoApp', ['helperModule']);你确实声明了demoApp var demoApp = angular.module('demoApp', ['helperModule']);

解决此问题的更好方法是只声明一次具有所有依赖项的应用程序,然后继续使用控制器、指令、工厂等组件对其进行修改。

代码

//could remove `helperModule` module if not used
var demoApp = angular.module('demoApp', ['helperModule', 'ngRoute']); 
demoApp.config(function($routeProvider) {
    $routeProvider
        .when('/', {
            controller: "SimpleController",
            templateUrl: "views/view1.html"
        })
        .when('partial2', {
            controller: "SimpleController",
            templateUrl: "views/view2.html"
        })
        .otherwise({
            redirectTo: "/"
        })
});

demoApp.controller("CustomersController", function($scope) {
    $scope.customers = [{
        name: 'Dave Jones',
        city: 'pheonix'
    }, {
        name: 'Dave Jones',
        city: 'New york'
    }, {
        name: 'Jones suman',
        city: 'pheonix'
    }, {
        name: 'Naresh babu',
        city: 'Hyderabad'
    }];

    $scope.addCustomer = function() {
        $scope.customers.push({
            name: $scope.newCustomer.name,
            city: $scope.newCustomer.city
        });
    };
});
于 2015-03-30T21:31:57.820 回答