将角度模块分解为组件

将角度模块分解为组件

问题描述:

我首先将所有东西都放到了一个app.js中,但这不是一个好方法。所以我想分成不同的文件,我认为最好的方法是制作3个不同的文件(因为我只有控制器和服务)。 主入口点app.js,然后是controllers.js和services.js 所以读完后应该定义主模块,然后使用其他模块作为依赖关系,所以我做到了。但它不起作用。 下面是该文件的定义:将角度模块分解为组件

app.js:

angular.module("personasApp", ["ngRoute", "personasApp.services", "personasApp.controllers"]) 
    .config(function($routeProvider) { 
     $routeProvider 
      .when("/", { 
       controller: "ListController", 
       templateUrl: "list.html", 
       resolve: { 
        personas: function(Personas) { 
         return Personas.getPersonas(); 
        } 
       } 
      }) 
      .when("/facturas/nueva", { 
       controller: "CargarFacturaControlador", 
       templateUrl: "facturas-form.html" 
      }) 
      .when("/personas/nueva", { 
       controller: "NuevaPersonaControlador", 
       templateUrl: "contact-form.html" 
      }) 
      .when("/personas/:personaId", { 
       controller: "EditarPersonaControlador", 
       templateUrl: "contact.html" 
      }) 
      .otherwise({ 
       redirectTo: "/" 
      }) 
     }); 

controllers.js

angular.module("personasApp.controllers", ["ngRoute"]) 
    .controller("ListController", function(personas, $scope) { 
     $scope.personas = personas.data; 
    }); 
    .controller("CargarFacturaControlador", function($scope, $location, Facturas) { 
     $scope.atras = function() { 
      $location.path("#/"); 
     } 

     $scope.cargarFactura = function(factura) { 
      Facturas.cargarFactura(factura).then(function(doc) { 
       var facturaUrl = "/facturas/" + doc.data._id; 
       $location.path(facturasUrl); 
      }, function(response) { 
       alert(response); 
      }); 
     } 
    }) 
    .controller("NuevaPersonaControlador", function($scope, $location, Personas) { 
     $scope.atras = function() { 
      $location.path("#/"); 
     } 

     $scope.guardarPersona = function(persona) { 
      Personas.crearPersona(persona).then(function(doc) { 
       var personaUrl = "/personas/" + doc.data._id; 
       $location.path(personaUrl); 
      }, function(response) { 
       alert(response); 
      }); 
     } 
    }) 
    .controller("EditarPersonaControlador", function($scope, $routeParams, Personas) { 
     Personas.getPersona($routeParams.personaId).then(function(doc) { 
      $scope.persona = doc.data; 
     }, function(response) { 
      alert(response); 
     }); 

     $scope.toggleEdit = function() { 
      $scope.editMode = true; 
      $scope.contactFormUrl = "contact-form.html"; 
     } 

     $scope.atras = function() { 
      $scope.editMode = false; 
      $scope.contactFormUrl = ""; 
     } 

     $scope.guardarPersona = function(persona) { 
      Personas.editarPersona(persona); 
      $scope.editMode = false; 
      $scope.contactFormUrl = ""; 
     } 

     $scope.borrarPersona = function(personaId) { 
      Personas.borrarPersona(personaId); 
     } 
    }); 

services.js

angular.module("personasApp.services", ["ngRoute"]) 
    .service("Facturas", function($http) { 
     this.cargarFactura = function(factura) { 
      return $http.post("/facturas", factura). 
       then(function(response) { 
        return response; 
       }, function(response) { 
        alert("Error cargando factura."); 
       }); 
     } 
    }) 
    .service("Personas", function($http) { 
     this.getPersonas = function() { 
      return $http.get("/personas"). 
       then(function(response) { 
        return response; 
       }, function(response) { 
        alert("Error intentando encontrar personas."); 
       }); 
     } 
     this.crearPersona = function(persona) { 
      return $http.post("/personas", persona). 
       then(function(response) { 
        return response; 
       }, function(response) { 
        alert("Error creando persona."); 
       }); 
     } 
     this.getPersona = function(personaId) { 
      var url = "/personas/" + personaId; 
      return $http.get(url). 
       then(function(response) { 
        return response; 
       }, function(response) { 
        alert("No se pudo encontrar esta persona."); 
       }); 
     } 
     this.editarPersona = function(persona) { 
      var url = "/personas/" + persona._id; 
      //console.log(contact._id); 
      return $http.put(url, persona). 
       then(function(response) { 
        return response; 
       }, function(response) { 
        alert("Error al editar esta persona."); 
        console.log(response); 
       }); 
     } 
     this.borrarPersona = function(personaId) { 
      var url = "/personas/" + personaId; 
      return $http.delete(url). 
       then(function(response) { 
        return response; 
       }, function(response) { 
        alert("Error al borar esta persona."); 
        console.log(response); 
       }); 
     } 
    }) 
); 

从我的index.html我使用脚本app.js

+1

定义*“不起作用”*。你得到什么错误?注意 - 你真的只需要注入'ngRoute'一次。所有注射都可以在整个应用程序中使用,但不应该导致它失败 – charlietfl

+0

@charlietfl对不起,应用程序应该做的就是通过路由提供程序访问这些表单,但它们不显示 – Fjallbacka

+0

ok ......但是没有提到脚本错误抛出,你也可以设置一个路由更改错误处理程序 – charlietfl

从我的index.html我使用的脚本app.js

你一定要包括所有文件到HTML页面,如果你想使他们的工作。如果你添加了单个文件,你的services.jscontrollers.js就不会被执行。

+0

我也试过,但仍然是相同的。我只在访问网页时得到这个呈现,但没有显示表单 – Fjallbacka

+0

你是对的,显然我有一个语法错误,当我打开Chrome的调试器时,我意识到了。谢谢 – Fjallbacka