-1

在示例 ToDo 项目之后,我正在使用 Angular 和 Breeze 测试示例 CRUD。但由于某种原因,我收到错误无法调用未定义的方法“getAll”。(我的 odata 托管在另一个 localhost 服务器上,并且启用了 CORS,我已经对其进行了测试)

这是我的代码:

主.js:

var app = {};

app.adminMuscleGroup = angular.module('WebApp', []);

数据服务:

app.adminMuscleGroup.dataService = (function (breeze, logger) {

    breeze.config.initializeAdapterInstances({ dataService: "OData" });

    var servicename = 'http://localhost:23758/odata/';

    var manager = new breeze.EntityManager(servicename);

    manager.enableSaveQueuing(true);

    var dataService = {
        getAll: getAll,

    };

    return dataService;

    function getAll() {
        var query = breeze.EntityQuery.from("MuscleGroup").orderBy("Name");

        return manager.executeQuery(query);
    }



})(breeze, app.logger);

控制器:

app.adminMuscleGroup.controller('AdminMuscleGroupCtrl', function($scope) {
    var dataService = window.app.dataService;
    var logger = window.app.logger;

    $scope.items = [];

    $scope.getAllMuscleGroups = function () {
        dataService.getAll()
            .then(querySucceeded)
            .fail(queryFailed);
    };

    $scope.getAllMuscleGroups();

    function querySucceeded(data) {
        $scope.items = [];
        data.results.forEach(function (item) {
            $scope.items.push(item);
        });

        $scope.apply();

        logger.info("Fetched all Muscle Groups");
    }

    function queryFailed(error) {
        logger.error(error.message, "Query failed");
    }

}

这是整个错误:

ypeError: Cannot call method 'getAll' of undefined
    at Object.$scope.getAllMuscleGroups (http://localhost:7122/Scripts/app/AdminMuscleGroup/MuscleGroupController.js:10:21)
    at new <anonymous> (http://localhost:7122/Scripts/app/AdminMuscleGroup/MuscleGroupController.js:15:12)
    at invoke (http://localhost:7122/Scripts/angular/angular.js:2902:28)
    at Object.instantiate (http://localhost:7122/Scripts/angular/angular.js:2914:23)
    at http://localhost:7122/Scripts/angular/angular.js:4805:24
    at http://localhost:7122/Scripts/angular/angular.js:4384:17
    at forEach (http://localhost:7122/Scripts/angular/angular.js:137:20)
    at nodeLinkFn (http://localhost:7122/Scripts/angular/angular.js:4369:11)
    at compositeLinkFn (http://localhost:7122/Scripts/angular/angular.js:4015:15)
    at publicLinkFn (http://localhost:7122/Scripts/angular/angular.js:3920:30) angular.js:5754
(anonymous function)
4

2 回答 2

0

尝试将dataService直接注入控制器

app.adminMuscleGroup.controller('AdminMuscleGroupCtrl', function($scope, dataService) {
    var dataService = window.app.dataService; // -> delete this
于 2013-08-02T23:09:45.227 回答
0

这是愚蠢的错误,我改变了这一行:

var dataService = window.app.dataService;

进入这个:

var dataService = window.app.adminMuscleGroup.dataService;

现在它可以工作了

于 2013-08-03T09:46:27.533 回答