当前位置: 首页 > 面试题库 >

创建工厂后无数据显示

田俊爽
2023-03-14
问题内容

这是我学习AngularJS的第二天。我有一个我无法解决的问题。一切顺利,直到我创建了工厂。我了解工厂/服务用于重构代码。因此,我从控制器中获取了一些代码,并将其发送到工厂,以便可以加载一些数据,但是事实并非如此。这些是我的代码:

index.html

(function() {
    var customerFactory = function() {
        var customers = [
          { 
            id: 1,
            joined: '2005-09-07', 
            name: 'Mayweather', 
            city: 'Brooklyn', 
            orderTotal: '43.1299',
            orders: [
              {
                id: 1,
                product: 'Pencils',
                total: 9.9956
              }
            ]

          }, 
          {
            id: 2,
            joined: '2005-09-07', 
            name: 'Jason', 
            city: 'Cleveland', 
            orderTotal: '89.8933',
            orders: [
              {
                id: 1,
                product: 'iPad',
                total: 20.9956
              }
            ]  
          }, 
          {
            id: 3,
            joined: '1999-08-27', 
            name: 'Jade', 
            city: 'Wroclaw', 
            orderTotal: '77.0092',
            orders: [
              {
                id: 1,
                product: 'Pillows',
                total: 12.2311
              }
            ]
          }, 
          {
            id: 4,
            joined: '2015-09-01', 
            name: 'David', 
            city: 'Accra', 
            orderTotal: '13.8465',
            orders: [
              {
                id: 1,
                product: 'Serek',
                total: 11.4782
              }
            ]
          }, 
          {
            id: 5,
            joined: '2001-01-18', 
            name: 'Doyet',
            city: 'Paris',
            orderTotal: '23.9930',
            orders: [
              {
                id: 1,
                product: 'Serek',
                total: 11.4782
              }
            ]
          }];

            //Defind factory object
            var factory = {};

            factory.getCustomers = function() {
                return customers;
            };

            return factory;

    };

            angular.module('myApp').factory('customerFactory', customerFactory);
}());

app.js

    angular.module('myApp', ['ngRoute'])
      .config(function ($routeProvider) {
        'use strict';
        $routeProvider
            .when('/', {
                controller: 'CustomersController',
                templateUrl: 'app/views/customers.html'
            })
            .when('/orders/:customerId', {
                controller: 'OrdersController',
                templateUrl: 'app/views/orders.html'
            })
            .otherwise( { redirectTo: '/' });
      });

##customers.html
<h3>Customers</h3>
Filter: <input type="text" ng-model="customerFilter.name" />
<br/><br/>

<table>
    <tr>
        <th ng-click="doSort('name')">Name</th>
        <th ng-click="doSort('city')">City</th>
        <th ng-click="doSort('orderTotal')">Order Total</th>
        <th ng-click="doSort('joined')">Joined</th>
        <th>&nbsp;</th>
    </tr>
    <tr ng-repeat="cust in filtered = (customers | filter:customerFilter | orderBy:sortBy:reverse)">
        <td>{{ cust.name | uppercase }}</td>
        <td>{{ cust.city }}</td>
        <td>{{ cust.orderTotal | currency:'PLN' }}</td>
        <td>{{ cust.joined | date}}</td>
        <td><a href="#/orders/{{ cust.id }}">View Orders</a></td>
    </tr>
</table>

<span> Total customers: {{ filtered.length }} </span>

customerController.js

angular.module('myApp')
  .controller('CustomersController', function ($scope, customersFactory) { 
    'use strict';

    $scope.sortBy = 'name';
    $scope.reverse = false;
    $scope.customers = [];

    function init (){
        $scope.customers = customersFactory.getCustomers();
    }

    init();

    $scope.doSort = function (propName) {
        $scope.sortBy = propName;
        $scope.reverse = !$scope.reverse;
    };
});

orders.html

<h3>Orders</h3>
<table>
    <tr>
        <th>Product</th>
        <th>Total</th>
    </tr>
    <tr ng-repeat="order in orders">
        <td>{{ order.product }}</td>
        <td>{{ order.total | currency:'PLN' }}</td>
    </tr>
</table>

customerFactory.js

(function() {
    var customerFactory = function() {
        var customers = [
          { 
            id: 1,
            joined: '2005-09-07', 
            name: 'Mayweather', 
            city: 'Brooklyn', 
            orderTotal: '43.1299',
            orders: [
              {
                id: 1,
                product: 'Pencils',
                total: 9.9956
              }
            ]

          }, 
          {
            id: 2,
            joined: '2005-09-07', 
            name: 'Jason', 
            city: 'Cleveland', 
            orderTotal: '89.8933',
            orders: [
              {
                id: 1,
                product: 'iPad',
                total: 20.9956
              }
            ]  
          }, 
          {
            id: 3,
            joined: '1999-08-27', 
            name: 'Jade', 
            city: 'Wroclaw', 
            orderTotal: '77.0092',
            orders: [
              {
                id: 1,
                product: 'Pillows',
                total: 12.2311
              }
            ]
          }, 
          {
            id: 4,
            joined: '2015-09-01', 
            name: 'David', 
            city: 'Accra', 
            orderTotal: '13.8465',
            orders: [
              {
                id: 1,
                product: 'Serek',
                total: 11.4782
              }
            ]
          }, 
          {
            id: 5,
            joined: '2001-01-18', 
            name: 'Doyet',
            city: 'Paris',
            orderTotal: '23.9930',
            orders: [
              {
                id: 1,
                product: 'Serek',
                total: 11.4782
              }
            ]
          }];

            //Defind factory object
            var factory = {};

            factory.getCustomers = function() {
                return customers;
            };

            return factory;

    };

            angular.module('myApp').factory('customerFactory', customerFactory);
}());

在Chrome的控制台中,我知道该customers.html文件未加载。我该如何解决?

安慰

XMLHttpRequest cannot load file:///Users/siaw/Desktop/angularjshelloworld/app/views/customers.html. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.(anonymous function) @ angular.js:10661
angular.js:12416 Error: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file:///Users/siaw/Desktop/angularjshelloworld/app/views/customers.html'.
    at Error (native)
    at https://code.angularjs.org/1.4.5/angular.js:10661:11
    at sendReq (https://code.angularjs.org/1.4.5/angular.js:10480:9)
    at serverRequest (https://code.angularjs.org/1.4.5/angular.js:10187:16)
    at processQueue (https://code.angularjs.org/1.4.5/angular.js:14634:28)
    at https://code.angularjs.org/1.4.5/angular.js:14650:27
    at Scope.$eval (https://code.angularjs.org/1.4.5/angular.js:15878:28)
    at Scope.$digest (https://code.angularjs.org/1.4.5/angular.js:15689:31)
    at Scope.$apply (https://code.angularjs.org/1.4.5/angular.js:15986:24)
    at bootstrapApply (https://code.angularjs.org/1.4.5/angular.js:1658:15)(anonymous function) @ angular.js:12416
angular.js:12416 Error: [$compile:tpload] Failed to load template: app/views/customers.html (HTTP status: undefined undefined)
http://errors.angularjs.org/1.4.5/$compile/tpload?p0=app%2Fviews%2Fcustomers.html&p1=undefined&p2=undefined
    at angular.js:68
    at handleError (angular.js:17565)
    at processQueue (angular.js:14634)
    at angular.js:14650
    at Scope.$eval (angular.js:15878)
    at Scope.$digest (angular.js:15689)
    at Scope.$apply (angular.js:15986)
    at bootstrapApply (angular.js:1658)
    at Object.invoke (angular.js:4473)
    at doBootstrap (angular.js:1656)

我将不胜感激。

编辑:

我安装了“ http-server”,现在得到以下信息:

安慰:

Error: [$injector:unpr] Unknown provider: customersFactoryProvider <- customersFactory <- CustomersController
http://errors.angularjs.org/1.4.5/$injector/unpr?p0=customersFactoryProvider%20%3C-ustomersFactory%20%3C-%20CustomersController
    at REGEX_STRING_REGEXP (https://code.angularjs.org/1.4.5/angular.js:68:12)
    at https://code.angularjs.org/1.4.5/angular.js:4284:19
    at Object.getService [as get] (https://code.angularjs.org/1.4.5/angular.js:4432:39)
    at https://code.angularjs.org/1.4.5/angular.js:4289:45
    at getService (https://code.angularjs.org/1.4.5/angular.js:4432:39)
    at invoke (https://code.angularjs.org/1.4.5/angular.js:4464:13)
    at Object.instantiate (https://code.angularjs.org/1.4.5/angular.js:4481:27)
    at https://code.angularjs.org/1.4.5/angular.js:9108:28
    at $route.link (http://localhost:8080/angular-route.js:977:26)
    at invokeLinkFn (https://code.angularjs.org/1.4.5/angular.js:8746:9)(anonymous function) @ angular.js:12416ident.$get @ angular.js:9203invokeLinkFn @ angular.js:8748nodeLinkFn @ angular.js:8246compositeLinkFn @ angular.js:7637publicLinkFn @ angular.js:7512name.$get.boundTranscludeFn @ angular.js:7656controllersBoundTransclude @ angular.js:8273update @ angular-route.js:935parent.$get.Scope.$broadcast @ angular.js:16200(anonymous function) @ angular-route.js:619processQueue @ angular.js:14634(anonymous function) @ angular.js:14650parent.$get.Scope.$eval @ angular.js:15878parent.$get.Scope.$digest @ angular.js:15689parent.$get.Scope.$apply @ angular.js:15986done @ angular.js:10511completeRequest @ angular.js:10683requestLoaded @ angular.js:10624
favicon.ico:1 GET http://localhost:8080/favicon.ico 404 (Not Found)

这意味着我的代码有问题。有人可以指出我的方向吗?为什么我的“提供者”未知?


问题答案:

我建议http-server作为一种快速简便的解决方案来运行您的应用程序,并帮助解决您的文件加载问题:

https://www.npmjs.com/package/http-
server



 类似资料:
  • 我有以下代码: 我将添加更多类似的类。与其单独拼出所有这些类,我想学习如何创建一个可以从简单的数据结构创建这些类的工厂。 我该怎么做?我阅读了Metaobject协议文档,但根据文档页面顶部和中间的示例,我不知道如何动态地为我的类指定不同的名称。 我试过: 但最后一行只是抛出了一个错误:

  • 我尝试使用HikariCP池连接,但我无法启动应用程序,因为我一直在创建sesion中获取空对象。 这是我的配置: 系统 Hibernate4.3.6。最终 MariaDB v10.0(x64) HibernateUtil类 hibernate.cfg.xml 日志 正在准备使用过滤器构建会话工厂:{}hh000130:实例化显式连接提供程序:org.mariadb.jdbc。应用程序启动方法中的

  • 我必须根据tenantIdentifier创建一个数据源bean以实现多租户。我正在考虑开箱即用的解决方案,添加新租户就像在context.xml中添加配置和在应用程序属性文件中添加租户属性一样简单,公开一个API来刷新我的context.xml以便从spring cloud config和属性文件中加载。 目前,我被这个错误所困扰: 我的DataSourceFactoryBean如下所示: 我有

  • 数据库工厂类提供了一些方法来帮助你管理你的数据库。 Table of Contents 数据库工厂类 初始化数据库工厂类 创建和删除数据库 创建和删除数据表 添加字段 添加键 创建表 删除表 重命名表 修改表 给表添加列 从表中删除列 修改表中的某个列 类参考 初始化数据库工厂类 重要 由于数据库工厂类依赖于数据库驱动器,为了初始化该类,你的数据库驱动器必须已经运行。 加载数据库工厂类的代码如下:

  • 释义 数据源(DataSource)的概念来自于JDBC规范中,一个数据源表示针对一个数据库(或者集群)的描述,从数据源中我们可以获得N个数据库连接,从而对数据库进行操作。 每一个开源JDBC连接池都有对DataSource的实现,比如Druid为DruidDataSource,Hikari为HikariDataSource。但是各大连接池配置各不相同,配置文件也不一样,Hutool的针对常用的连