前端 - ng-view不能加载进模板
问题描述
在学习angularjs教程,ng-view 没有加载进模板,但是按照官方的写法又能加载进模板,我自己的写法不行!我的写法与官方的有啥区别,为啥不能加载进模板呢?下面是我的项目目录结构
app.js
’use strict’;/* App Module */angular.module(’phonecatApp’,[’ngRoute’]).config([’$routeProvider’,function($routeProvider) { $routeProvider .when(’/phones’,{ templateUrl:’partials/phone-list.html’,controller:’PhoneListCtrl’}) .when(’/phones/:phoneId’, { templateUrl:’partials/phone-detail.html’,controller:’PhoneDetailCtrl’}) .otherwise({redirectTo: ’/phones’});}]);
controller.js
angular.module(’phonecatApp’,[]).controller(’PhoneListCtrl’,[’$scope’,’$http’, function($scope, $http) { $http.get(’phones/phones.json’) .success(function(data) {$scope.phones = data.splice(0,5); }); $scope.orderProp = ’age’;}]).controller(’PhoneDetailCtrl’,[’$scope’,’$routeParams’,function($scope,$routeParams) { $scope.phoneId = $routeParams.phoneId;}]);官方教程上的写法
app.js
var phonecatApp = angular.module(’phonecatApp’, [ ’ngRoute’, ’phonecatControllers’]);phonecatApp.config([’$routeProvider’, function($routeProvider) { $routeProvider. when(’/phones’, {templateUrl: ’partials/phone-list.html’,controller: ’PhoneListCtrl’ }). when(’/phones/:phoneId’, {templateUrl: ’partials/phone-detail.html’,controller: ’PhoneDetailCtrl’ }). otherwise({redirectTo: ’/phones’ }); }]);
controller.js
var phonecatControllers = angular.module(’phonecatControllers’, []);phonecatControllers.controller(’PhoneListCtrl’, [’$scope’, ’$http’, function($scope, $http) { $http.get(’phones/phones.json’).success(function(data) { $scope.phones = data; }); $scope.orderProp = ’age’; }]);phonecatControllers.controller(’PhoneDetailCtrl’, [’$scope’, ’$routeParams’, function($scope, $routeParams) { $scope.phoneId = $routeParams.phoneId; }]);
问题解答
回答1:angular.module(’phonecatApp’,[])使用已存在的模块的时候不要加后面的依赖了。。。angular.module(’phonecatApp’)。。。这样就ok了!你上面那样类似重新定义了一个名为phonecatApp的模块,依赖是空[]。
回答2:module 重定义了,controller 里换个名字,app 中依赖它
相关文章:
1. sql语句 - 如何在mysql中批量添加用户?2. mysql - 数据库建字段,默认值空和empty string有什么区别 1103. 求大神支招,php怎么操作在一个html文件的<head>标记内添加内容?4. mysql建表报错,查手册看不懂,求解?5. 怎么php怎么通过数组显示sql查询结果呢,查询结果有多条,如图。6. javascript - mysql插入数据时怎样避免与库中的数据重复?7. mysql - JAVA怎么实现一个DAO同时实现查询两个实体类的结果集8. 致命错误: Class ’appfacadeTest’ not found9. phpstady在win10上运行10. php - 数据库表如果是null怎么替换为其他字段的值
