2015-11-20 3 views
1

На данный момент я получаю ошибку, упомянутую выше. Я думаю, что это странно, что я получаю эту ошибку, так как я не хочу использовать «маршрутизацию» из Angular, но просто использую угловые для публикации/получения данных.

Мои угловые функции разделены на модуль, сервис & файл контроллера. Кроме того, я использую двигатель под названием «EJS». Таким образом, это означает, что приведенный ниже список HTML выглядит немного неполным. Это связано с тем, что я включаю большую часть своего HTML из файла «layout», где загружаются основные элементы, такие как панель навигации.

HTML:

<% include ../layout %> 
 

 
     <body ng-app="clientModule"> 
 
      <div class="container" data-ng-cloak data-ng-app="clientModule" data-ng-controller="clientController"> 
 
       <form class="navbar-form navbar-left" role="search" method="POST" name="formClient"> 
 
        <div class="row"> 
 
         <div class="form-group"> 
 

 
          <label for="">Client name</label> 
 
          <input type="text" class="form-control" placeholder="Please enter client name" name="clientName" ng-model="client.clientName" style="width: 100%" required> 
 

 
         </div> 
 
        </div> 
 
        <div>&nbsp;</div> 
 
        <div class="row"> 
 
         <div class="form-group"> 
 

 
          <label for="">Client address</label> 
 
          <input type="text" class="form-control" placeholder="Please enter client address" name="clientName" ng-model="client.clientAddress" style="width: 100%" required> 
 

 
         </div> 
 
        </div> 
 
        <div>&nbsp;</div> 
 

 
        <div class="row"> 
 
         <div class="form-group"> 
 
          <button type="button" class="btn btn-primary" ng-lick="createClient(client)">Create client</button> 
 
         </div> 
 
        </div> 
 
       </form> 
 
      </div> 
 
     </body> 
 

 

 

 
     <script src="/bower_components/angular/angular.js"></script> 
 

 
     <script src="/bower_components/angular-route/angular-route.js"></script> 
 
     <script src="../../controllers/clients/clientModule.js"></script> 
 
     <script src="../../controllers/clients/clientService.js"></script> 
 
     <script src="../../controllers/clients/clientController.js"></script>

Модуль:

 

var clientModule = angular.module('clientModule', []); 

Услуги:

 

angular.module('clientModule').controller('clientService', clientService); 


clientService.$inject = ['$http']; 


function clientService($scope) { 

    return { 

     createClient : function (client) { 

      return $http.post('/createClient', 
       { 
       clientName : client.clientName, 
       clientAdress : client.clientAddress 
      } 
      ); 
     } 
    }; 
} 

Контроллер:

 

    angular.module('clientModule').controller('clientController', clientController); 


     clientController.$inject = ['$scope', '$timeout', 'clientService']; 


     function clientController($scope, $timeout, clientService) { 

      $scope.client = { 

       clientName : "", 
       clientAddress : "" 

      }; 

      $scope.client = function (client) { 

       clientService.createClient(client).success(function (data) { 

        /* $timeout(function() { 


         alert("data posted successfully"); 

        },3000) */ 
       }); 
      } 
     } 


NodeJS выход:

 
    $ node server 
    Server running on port: 1337 
    GET /createClient 304 18.255 ms - - 
    GET /stylesheets/bootstrap.min.css 304 6.576 ms - - 
    GET /bower_components/bootstrap/dist/js/bootstrap.js 304 9.184 ms - - 
    GET /bower_components/jquery/dist/jquery.min.js 304 10.352 ms - - 
    GET /controllers/clients/clientService.js 304 4.351 ms - - 
    GET /bower_components/angular/angular.js 304 6.103 ms - - 
    GET /controllers/clients/clientModule.js 304 2.677 ms - - 
    GET /bower_components/angular-route/angular-route.js 304 3.597 ms - - 
    GET /controllers/clients/clientController.js 304 1.758 ms - - 
    GET /bower_components/jquery/dist/jquery.min.map 304 1.767 ms - - 
    GET /bower_components/angular-route/angular-route.js 304 0.733 ms - - 
    GET /controllers/clients/clientModule.js 304 0.755 ms - - 
    GET /controllers/clients/clientService.js 304 0.939 ms - - 
    GET /controllers/clients/clientController.js 304 1.101 ms - - 

JQuery файл маршрутизации:

 
function clientRouteConfig(app) { 

    this.app = app; 
    this.routeTable = []; 
    this.init(); 

} 

clientRouteConfig.prototype.init = function() { 

    var self = this; 

    this.addRoutes(); 
    this.processRoutes(); 

} 

clientRouteConfig.prototype.processRoutes = function() { 

    var self = this; 

    self.routeTable.forEach(function(route) { 

     if(route.requestType == 'get') { 

      self.app.get(route.requestUrl, route.callbackFunction); 

     } 
     else if(route.requestType == 'post') { } 
     else if(route.requestType == 'delete') { } 

    }); 

} 

clientRouteConfig.prototype.addRoutes = function() { 

    var self = this; 

    self.routeTable.push({ 

     requestType : 'post', 
     requestUrl : '/createClient', 
     callbackFunction : function(request, response) { 

      response.render('../views/clients/createClient', { title: "Create client"}); 

     } 

    }); 

    var self = this; 

    self.routeTable.push({ 

     requestType : 'get', 
     requestUrl : '/createClient', 
     callbackFunction : function(request, response) { 

      response.render('../views/clients/createClient', { title: "Create client"}); 

     } 

    }); 


    self.routeTable.push({ 

     requestType : 'get', 
     requestUrl : '/clients', 
     callbackFunction : function(request, response) { 

      response.render('../views/clients/clients', { title: "Clients"}); 

     } 

    }); 

} 


module.exports = clientRouteConfig; 

Заранее спасибо!

+0

Где ваш файл маршрута? –

+0

Возможно, несвязанное предложение заключается в создании вашего сервиса с помощью .service() вместо .controller(). Не уверен, что это только опечатка в коде, который вы разместили здесь. – mgiesa

+0

Я не использую Угловую маршрутизацию. Я следую этому руководству, где он использует jQuery для маршрутизации. Я собирался использовать это сначала, а затем изменить его позже, когда узнаю больше о угловых. Однако я добавил файл маршрутизации jQuery. Кроме того, я изменил 'controller – Mick

ответ

1

В моем файле «layout» я вызвал другое приложение (старый код), вызвавшее ошибку. Теперь все исправлено :)!

Спасибо всем за сотрудничество.