1

对 MeanJS 和 Angular 来说仍然很新,但我正在尝试让中继器使用我创建的自定义节点服务

这是角度模板

<section data-ng-controller="AppController">
    <section data-ng-controller="GroupsController" data-ng-init="findMyItems()">
        <div class="page-header">
            <h1>My Groups</h1>
        </div>
        <div class="list-group">
            <a data-ng-repeat="group in groups" data-ng-href="#!/groups/{{group._id}}" class="list-group-item">

                <small class="pull-right" data-ng-bind="group.shortId"></small>
                <h4 class="list-group-item-heading" data-ng-bind="group.name"></h4>
                <small class="list-group-item-text">
                    Posted on
                    <span data-ng-bind="group.created | date:'medium'"></span>
                    by
                    <span data-ng-bind="group.user.displayName"></span>
                </small>
            </a>
        </div>
        <div class="alert alert-warning text-center" data-ng-hide="!groups.$resolved || groups.length">
            No Groups yet, why don't you <a href="/#!/groups/create">create one</a>?
        </div>
    </section>
</section>

这是从 localhost:3000/users/me/groups 返回的 JSON 对象数组

[
{
_id: "5407dd31594e810000af4fa0",
user: "5407c78f9ef3025bbf0440f7",
description: "Activating....",
__v: 0,
projects: [ ],
created: "2014-09-04T03:32:01.825Z",
shortId: "bkXtE746M",
name: "Wonder Twins"
},
{
_id: "5407dc49a34a610000af6896",
user: "5407c78f9ef3025bbf0440f7",
description: "Loved watching this one",
__v: 0,
projects: [ ],
created: "2014-09-04T03:28:09.480Z",
shortId: "WJejxZorTz",
name: "Fantastic Four"
},
{
_id: "5407d71839c7de000008cf6b",
user: "5407c78f9ef3025bbf0440f7",
description: "Great group",
__v: 0,
projects: [ ],
created: "2014-09-04T03:06:00.098Z",
shortId: "ZJfKDyN6f",
name: "Leaders of the New School"
}
]

控制器

'use strict';

// Groups controller
angular.module('groups').controller('GroupsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Groups', 'GroupsAPI',
    function($scope, $stateParams, $location, Authentication, Groups, GroupsAPI ) {
        $scope.authentication = Authentication;

        $scope.findMyItems = function() {
        GroupsAPI.getGroupsByCurrentUser()
            .success(function (groups) {
                $scope.groups = groups;
            })
            .error(function (error) {
                $scope.status = 'Unable to load group data: ' + error.message;
            });
    };

    }
]);

我不确定 MeanJS 中的服务在做什么

'use strict';

//Groups service used to communicate Groups REST endpoints

angular.module('groups').factory('Groups', ['$resource',
    function($resource) {
        return $resource('groups/:groupId', { groupId: '@_id'
        }, {
            update: {
                method: 'PUT'
            }
        });
    }
]);

我想做的是类似于下面这样的事情,但不确定是否有更好的方法

'use strict';

//Groups service used to communicate Groups REST endpoints
angular.module('groups').factory('Groups', ['$resource',
    function($resource) {
        return $resource('groups/:groupId', { groupId: '@_id'
        }, {
            update: {
                method: 'PUT'
            }
        });
    }
]);


angular.module('groups')
    .factory('GroupsAPI', ['$http', function($http) {

        var GroupsAPI = {};

        GroupsAPI.getGroupsByCurrentUser = function () {
            return $http.get('users/me/groups');
        };

        return GroupsAPI;
    }]);

有没有更好的方法来做到这一点 MeanJS 方式?

4

1 回答 1

0

自从您发布此内容以来已经很长时间了,所以您可能已经想出了一个解决方案,但是为了未来的读者,我会在这里给出一个答案。

如果我理解您正在尝试做什么,那么您似乎正在尝试创建一个功能类似于现有Groups工厂的自定义工厂(您提到的那个您不知道它在做什么)。这就是我要回答的...

首先,您需要阅读 Angular 文档$resourcehttps ://docs.angularjs.org/api/ngResource/service/$resource 。这就是Groups工厂运作的原因。

总而言之,Angular$resource允许您非常轻松地发出 AJAX 请求,方法是允许您在控制器中创建一个可以访问 REST 函数的变量。所以基本上,你会做这样的事情:

// Groups controller
angular.module('groups').controller('GroupsController', ['$scope', '$stateParams', '$location', 'Authentication', 'Groups', 'GroupsAPI',
function($scope, $stateParams, $location, Authentication, Groups, GroupsAPI ) {
    $scope.authentication = Authentication;

    // Since you've added 'Groups' as a dependency, you can now use this "resource" in a new variable.
    var groups = new Groups({
        // Set any object data you need here, or leave empty.
    });

    // Now that you have a new instance of your 'Groups' resource (i.e. 'groups'), you can use standard REST calls.
    groups.get({
        user: currentUser
    }, function(results) {

        // Do something with results here, e.g.
        if(results) {
            $scope.groups = results;
        } else {
            $scope.status = 'Unable to load  group data.';
        }

    });
]);

注意:我没有测试过这段代码,但这就是想法。

于 2015-06-02T21:10:07.407 回答