0

我为我的项目数据创建了一个“模型”*,如下所示:

angular.module('myapp').factory("projectModel", function ($resource) {
    return $resource(
        "/api/projects/:id",
        { id: "@id" },
        {
            "update": { method: "PUT" }
        }
    );
});

希望在路由中使用 wait-until-everything-has-loaded-before-displaying-anything 行为resolve,相反我很乐意让 UI 和数据在它准备好时弹出。

将项目数据放在$scope我的控制器中会很好,但是由于projectModel返回一个promise而不是data,我需要执行以下操作:

angular.module('myapp').controller('EditorCtrl', function ($scope, projectModel) {

    projectModel.get({}, { 'id': session.projectId },
        function (successResponse) {
            // success callback
            $scope.project = successResponse;
        },
        function (errorResponse) {
            // failure callback
            console.log(errorResponse);
        }
    );

数据会在指令中渲染,但是由于设置了指令的link方法执行之前$scope.project,我没有数据可以及时渲染。我可以包含projectModel对指令的依赖,但这感觉很脏。

使指令可以访问项目数据的正确方法是什么?

*额外问题:我应该将我的$resource衍生服务视为 MVC 模型,还是令人困惑?如果是,使用辅助方法等扩展模型的最佳方法是什么?

4

1 回答 1

1
$scope.$watch('projectList', function (newVal, oldVal) {
                    if (newVal !== oldVal ) {
                    angular.forEach($scope.projectList, function (row) {
                          row.getName = function () {
                            return splitName(row.community_name);
                          };
                       });
                    }
                }, true);

Yes, you are right I had similar issue, you can do this by watch check for change in $scope.project and you can put the code inside it. Check the above example to update ng-grid dynamically when there is value in $scope.projectList

于 2014-02-14T10:28:03.533 回答