我正在尝试使用angularjs中的routeproviders动态加载两个控制器。但是它仅返回空白页,而不显示默认页。我做了什么错误?代码如下。

Index.html

<html ng-app="demoController">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>

<body>
    <div ng-view=""></div>
</body>
<script>
    var demoController = angular.module('demoController',[]);

    demoController.config = function ($routeProvider){
        $routeProvider
            .when('/customers',{
                controller:'sampleController',
                templateUrl:'template/customers.html'
            })

            .when('/location',{
                controller:'nextController',
                templateUrl:'template/location.html'
            })
            .otherwise({redirect:'/customers'});
    });

    var dumpControllers={};
    dumpControllers.sampleController=function ($scope){
        $scope.customers=[
            {name:'John Smith', country:'Denmark', worth:'5000000'},
            {name:'John Lewis',country:'England',worth:'10000000'},
            {name:'Rick Evans',country:'America',worth:'6000000'}
        ];
    };

    dumpControllers.nextController=function ($scope){
        $scope.location=[{city:'Chennai'},{city:'Bangalore'},{city:'Mumbai'}];
    }
    demoController.controller(dumpControllers);
</script>




location.html

        <ul>
            <li ng-repeat="loc in location | orderBy: 'city'">{{ loc.city }}</li>
        </ul>


customer.html

        <input type="text" ng-model="searchName">
        <ul>
            <li ng-repeat="cust in customers | filter:searchName | orderBy:'country'">{{ cust.name }} - {{ cust.country }} - {{ cust.worth | currency:"$":2 }}</li>
        </ul>


可以帮助我完成这项工作吗?谢谢!!!

最佳答案

加载角度后,将其放在头上:

<script src="https://code.angularjs.org/1.3.9/angular-route.js"></script>


并注入模块:

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


还有另一个问题:

        .when('/location',{
            controller:'nextController',
            templateUrl:'template/location.html'
        })
        .otherwise({redirect:'/customers'});
};//Removed parenthesis

关于javascript - angularjs:带有routeproviders的 Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29351901/

10-12 07:20