search

Home  >  Q&A  >  body text

前端 - 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;
 }]);
PHP中文网PHP中文网2744 days ago560

reply all(2)I'll reply

  • 高洛峰

    高洛峰2017-05-15 16:59:16

    angular.module('phonecatApp',[]) When using an existing module, do not add subsequent dependencies. . .
    angular.module('phonecatApp'). . . That’s ok!
    You redefined a module named phonecatApp similarly to the above, and the dependency is empty [].

    reply
    0
  • 天蓬老师

    天蓬老师2017-05-15 16:59:16

    Module has been redefined, change the name in the controller, and the app depends on it

    reply
    0
  • Cancelreply