2

今日のコードは、特に Angular サービスの Revealing Module パターンを使用した Angular で再び私を怒らせています。私が始めたとき、私はそれについて何の問題も見ませんでした...今は機能していないことを除けば...わかりません。それが角張っているのか、それとも私が何か愚かなことをしているのかを知りたいと思っています。コードは次のとおりです。

angular.module('homeAdmin.services', [])
.factory('dataService', function ($http, $q) {
    'use strict';
    function _contentTypes(){
        var intialized = false;
        var _models = [];
        function _isReady() {
            return intialized;
        };
        return {
            isReady: _isReady
        };
    };

    return {
        contentTypes: _contentTypes
    };
});

そして、ここでそれが呼び出されます:

var contentTypeCtrl = ['$scope','$http','$window','dataService', function ($scope, $http, $window, dataService) {
'use strict';
$scope.isBusy = false;
$scope.data = dataService;
$scope.contentType = {};

$scope.name = 'Content Type';
$scope.init = function () {
    console.log('contentTypeCtrl initialized');
};

if (dataService.contentTypes.isReady() == false) {
    console.log("Hello let's load some data!"); 
}}]

そして、これがFirefoxが私に指を与える方法です:

[16:01:56.864] "Error: dataService.contentTypes.isReady is not a function
contentTypeCtrl<@http://localhost:49499/App/plugins/home/admin/ngen/content-type-ctrl.js:12
invoke@http://localhost:49499/Scripts/angular/angular.js:2902
instantiate@http://localhost:49499/Scripts/angular/angular.js:2914
@http://localhost:49499/Scripts/angular/angular.js:4805
updateView@http://localhost:49499/Scripts/angular/angular-ui-router.js:1317
$ViewDirective/directive.compile/</eventHook@http://localhost:49499/Scripts/angular/angular-ui-router.js:1276
Scope.prototype.$broadcast@http://localhost:49499/Scripts/angular/angular.js:8307
$StateProvider/$get/transitionTo/$state.transition<@http://localhost:49499/Scripts/angular/angular-ui-router.js:1067
qFactory/defer/deferred.promise.then/wrappedCallback@http://localhost:49499/Scripts/angular/angular.js:6846
qFactory/ref/<.then/<@http://localhost:49499/Scripts/angular/angular.js:6883
Scope.prototype.$eval@http://localhost:49499/Scripts/angular/angular.js:8057
Scope.prototype.$digest@http://localhost:49499/Scripts/angular/angular.js:7922
Scope.prototype.$apply@http://localhost:49499/Scripts/angular/angular.js:8143
done@http://localhost:49499/Scripts/angular/angular.js:9170
completeRequest@http://localhost:49499/Scripts/angular/angular.js:9333
createHttpBackend/</xhr.onreadystatechange@http://localhost:49499/Scripts/angular/angular.js:9304"

ヘルプ?誰??

isready 関数の使用例を次に示します。

    if (dataService.contentTypes.isReady() == false) {
    $scope.isBusy = true;
    dataService.contentTypes.get()
        .then(function () {
        }, function () {
            alert('contentTypes retrieval failed');
        })
        .then(function () {
            $scope.isBusy = false;
        });
}

そして、次のようにデータを消費します。

<tr data-ng-repeat="model in data.contentTypes.models">
4

2 に答える 2

5

私は Angular に精通していないので、ここから外れていたらすみません (-!

contentTypes() を呼び出すことで isReady() が返されるように見えますが、contentTypes への呼び出しは表示されません。クロージャー fn _contentTypes を指す参照のみです。

これが代わりに以下のように読まれる可能性はありますか?

dataService.contentTypes().isReady()

* contentTypes 関数を実際に呼び出すために「()」を追加しました。これにより、isReady() 関数が返されます。

于 2013-09-08T15:01:54.843 に答える
1

Module Revealing Pattern をファクトリで使用することはできません。インスタンス化されたときにプロパティのデフォルト値のみが返されます。モジュールを公開するパターンを使用する場合は、サービスを使用する必要があります。

于 2015-04-26T21:55:26.413 に答える