角度在60分钟的路线问题

时间:2015-03-30 21:27:56

标签: javascript angularjs angularjs-controller angularjs-module

我想知道是否有人可以快速查看我的代码并让我知道我是否遗漏了任何东西。我正在尝试学习Angular.js,并且在60分钟的视频中使用着名的Angular.js。然而,我所遇到的问题是角度已经更新,因为事情有点不同。结果我无法正确地编排我的路线。非常感谢任何人提前。

我真正想知道的是,是否有任何机构可以告诉我,为了使这些路线正常工作,我错过了什么?

以下是我的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>

1 个答案:

答案 0 :(得分:4)

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

然后从var demoApp = angular.module('demoApp', ['helperModule']);开始 你再次宣布demoApp var demoApp = angular.module('demoApp', ['helperModule']);

解决此问题的更好方法是使用所有依赖关系&amp;继续用控制器,指令,工厂等组件修改它

<强>代码

//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
        });
    };
});