未知提供商,AngularJS

时间:2015-09-02 23:17:39

标签: javascript angularjs

我可能做错了什么?我想展示一些价值但似乎无法让它发挥作用。我尝试使用常量而没有任何工作。

以下是一些片段:

values.js

angular.module('myApp').value('appSettings', {
    title: 'Customers Application',
    version: '1.0'
});

/*angular.module('myApp').constant('appSettings', {
    title: 'Customers Application',
    version: '1.0'
});*/

customersController.js

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

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

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

    init();

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

customers.html客户

<h3> {{ appSettings.title }}</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>
<br/>
<footer>Version: {{ appSettings.version }}</footer>

JS控制台:

Error: [$injector:unpr] Unknown provider: appSettingsProvider <- appSettings <- CustomersController
http://errors.angularjs.org/1.4.5/$injector/unpr?p0=appSettingsProvider%20%3C-%20appSettings%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) <div ng-view="" class="ng-scope">

知道我的提供商是什么“未知”吗?这是我第二天学习Angular。

2 个答案:

答案 0 :(得分:1)

根据你在这里粘贴的内容,它是有效的。您确定要加载values.js吗?

答案 1 :(得分:-1)

我不是100%确定它会导致您收到的错误,但您粘贴的代码的一个问题是使用了angular.module()。第一次使用此函数时,使用两个参数调用它:名称和任何模块依赖项的数组。因此,首先加载的文件应该是这样的:

angular.module('myApp', [])