前端 - 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. dockerfile - 為什么docker容器啟動不了?2. angular.js - angularjs的自定義過濾器如何給文字加顏色?3. macos - mac下docker如何設置代理4. 請教各位大佬,瀏覽器點 提交實例為什么沒有反應5. javascript - iframe 為什么加載網頁的時候滾動條這樣顯示?6. dockerfile - 我用docker build的時候出現下邊問題 麻煩幫我看一下7. javascript - es6中this8. javascript - web網頁版app返回上一頁按鈕在ios設備失效怎么辦?安卓上可以,代碼如下,請大神幫助,萬分感謝。9. javascript - 用rem寫的頁面,安卓手機顯示文字是正常的,蘋果顯示的文字是特別小的是為什么呢10. 新手 - Python 爬蟲 問題 求助
