【问题标题】:Revealing Module Pattern with Angular用 Angular 揭示模块模式
【发布时间】:2013-09-08 14:14:54
【问题描述】:

今天的代码又让我发疯了,尤其是 Angular 服务中带有显示模块模式的 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">

【问题讨论】:

  • 当您针对它的包含值进行测试时,您确定数据服务已加载吗?另外,我从来没有像 AMD 这样的双重嵌套函数,但是当你使用 dataservice.contentTypes().isReady() 时结果是什么?
  • 是的,控制器第一行的断点表明 dataService 已完全注入并准备就绪...“结果是​​什么”是什么意思?当前使用导致上面给出的异常。从功能上讲,IsReady() 函数将检查 dataService 是否已经加载了该集合的数据。
  • 基本上我的意思是当你这样做时会发生什么(在 contentTypes 中添加了一个额外的参数),下面的 user1566994 人只是劫持了我所说的并将其作为答案:)

标签: javascript angularjs revealing-module-pattern


【解决方案1】:

我对 Angular 并不精通,所以如果我离开这里,请原谅 (-!

看起来 isReady() 是通过调用 contentTypes() 返回的 - 但我没有看到对 contentTypes 的调用 - 只有一个引用,它指向closure-fn _contentTypes。

是否有可能改为如下所示?

dataService.contentTypes().isReady()

*添加 '()' 来实际调用 contentTypes 函数,然后返回 isReady() 函数

【讨论】:

  • 把它钉在一个...谢谢!你不知道我应该改变什么才能省略那些“()”?
  • 这是一个有效的答案,我不明白为什么它被否决了?
  • 太棒了!很高兴我能帮上忙(-!
【解决方案2】:

您不能在工厂中使用模块显示模式, 它只会在实例化时返回属性的默认值。 如果要使用模块显示模式,则需要使用服务。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-12
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多