Front-end - ng-view cannot be loaded into the template
PHP中文网
PHP中文网 2017-05-15 16:57:16
0
2
573

While studying the angularjs tutorial, ng-view is not loaded into the template.
But it can be loaded into the template according to the official writing method, but my own writing method does not work!
What’s the difference between my writing method and the official one? Why can’t it be loaded into the template?
The following is my project directory structure

My way of writing

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;
}]);

How to write in the official tutorial

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中文网

认证0级讲师

reply all(2)
小葫芦

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 [].

Peter_Zhu

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template