我尝试显示带有 Angular 路径的 View 。控制台不会抛出任何错误或警告。
但同样该 View 也不会显示。 我做错了什么?
index.html
<!DOCTYPE html>
<html lang="en" ng-app="app">
<head></head>
<body>
<ng-view></ng-view>
<!-- build:js bower/vendor -->
<script type="text/javascript" src="../bower_components/angular/angular.min.js"></script>
<script src="../bower_components/angular-route/angular-route.min.js"></script>
<!-- endbuild -->
<script src="./routes.js"></script>
<!-- build:app scripts/js || CUSTOMER -->
<script src="./components/customer/customerService.js"></script>
<script src="./components/customer/customerController.js"></script>
<!-- endbuild-->
</body>
</html>
routes.js
var _templateBase = './components';
angular.module('app', [
'ngRoute'
])
.config(['$routeProvider', function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: _templateBase + '/customer/customer.html' ,
controller: 'customerController',
controllerAs: '_ctrl'
})
.otherwise({
redirectTo: '/'
});
}
]);
costomerService.js
angular.module('app', [])
.factory('customerService', function() {
});
costomerController.js
angular.module('app',[])
.controller('customerController', ['$scope', function($scope, customerService) {
//
}]);
有错吗?因为 View 没有显示?我正在使用过时的 mertodos,请指导我那里有很多教程。
谢谢他们可以提供帮助。
最佳答案
这会创建您的应用程序,因为您已包含第二个参数(要注入(inject)的依赖项数组):
angular.module('app', [
'ngRoute'
])
删除其他 angular.module()
定义中的第二个参数,因为这会导致每次创建新应用。
angular.module('app') <-- no second parameter tells it to use the existing 'app' module
.factory('customerService', function() {
});
angular.module('app') <-- no second parameter tells it to use the existing 'app' module
.controller('customerController', ['$scope', 'customerService', function($scope, customerService) {
//
}]);
我将 customerService
添加到 Controller 定义上的注入(inject)数组中,因为数组元素必须与您的函数参数完全匹配。
另外,如果您像在路由定义中那样使用 controllerAs
语法,那么您可能不希望将 $scope
注入(inject)到您的 Controller 中。
关于javascript - 不显示 AngularJS View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38670778/