A
A
Alex2015-10-25 01:22:19
Angular
Alex, 2015-10-25 01:22:19

How to configure route and services and controller?

I'm a newbie and I'm already tired of digging the Internet! need help!!!
have a service

'use strict';

var myApp = angular.module('myApp');

myApp.factory('Mans', ['$resource',function($resource){
  return $resource('/mans.json', {},{
    query: { method: 'GET', isArray: true },
    create: { method: 'POST' }
  })
}]);

myApp.factory('Man', ['$resource', function($resource){
  return $resource('/mans/:id.json', {}, {
    show: { method: 'GET' },
    update: { method: 'PUT', params: {id: '@id'} },
    delete: { method: 'DELETE', params: {id: '@id'} }
  });
}]);

there is a controller and a route (below)
'use strict';

var myApp = angular.module('myApp', ['ngRoute', 'ngResource']); 

//Controller
myApp.controller("ManListCtrl", ['$scope', '$http', '$resource', 'Mans', '$location', function($scope, $http, $resource, Mans, $location) {
  console.log("ManListCtrl");
  $scope.mans = Mans.query();
}]);

myApp.controller("ManNewCtrl", ['$scope', '$http', '$resource', 'Mans', '$location', '$routeParams', function($scope, $http, $resource, Mans, $routeParams,  $location) {
  consol.log("ManNewCtrl");
}]);

myApp.controller("ManDetailCtrl", ['$scope', '$resource', 'Man', '$location', '$routeParams', function($scope, $resource, Man, $location, $routeParams) {
  $scope.man = Man.show({id: $routeParams.id});
}]);

//Routes
myApp.config([
  '$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
    $routeProvider.when('/mans', {
      templateUrl: 'partials/man-index.html',
      controller: 'ManListCtrl'
    });
    $routeProvider.when('/mans/:id', {
      templateUrl: 'partials/man-detail.html',
      controller: 'ManDetailCtrl'
    });
    $routeProvider.when('/mans/new', {
      templateUrl: 'partials/man-new.html',
      controller: 'ManNewCtrl'
    });
    $routeProvider.otherwise({
      redirectTo: '/mans'
    });
  }
]);

why the ManNewCtrl controller doesn't work and the man-new.html template doesn't pull up,
it doesn't even output anything to the console!
but the ManListCtrl controller prints a message to the console 2 times!!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2015-10-25
@sashasoft

because the route with the ID overlaps it, falls under the rule. Either put the route above, or specify that only ints can be used as an ID.
read the angular-styleguide
This is not the point of the question, just comments on the code.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question