'tag.htm'; break; case 'flag': $pre .= $default_pre .= 'flag.htm'; break; case 'my': $pre .= $default_pre .= 'my.htm'; break; case 'my_password': $pre .= $default_pre .= 'my_password.htm'; break; case 'my_bind': $pre .= $default_pre .= 'my_bind.htm'; break; case 'my_avatar': $pre .= $default_pre .= 'my_avatar.htm'; break; case 'home_article': $pre .= $default_pre .= 'home_article.htm'; break; case 'home_comment': $pre .= $default_pre .= 'home_comment.htm'; break; case 'user': $pre .= $default_pre .= 'user.htm'; break; case 'user_login': $pre .= $default_pre .= 'user_login.htm'; break; case 'user_create': $pre .= $default_pre .= 'user_create.htm'; break; case 'user_resetpw': $pre .= $default_pre .= 'user_resetpw.htm'; break; case 'user_resetpw_complete': $pre .= $default_pre .= 'user_resetpw_complete.htm'; break; case 'user_comment': $pre .= $default_pre .= 'user_comment.htm'; break; case 'single_page': $pre .= $default_pre .= 'single_page.htm'; break; case 'search': $pre .= $default_pre .= 'search.htm'; break; case 'operate_sticky': $pre .= $default_pre .= 'operate_sticky.htm'; break; case 'operate_close': $pre .= $default_pre .= 'operate_close.htm'; break; case 'operate_delete': $pre .= $default_pre .= 'operate_delete.htm'; break; case 'operate_move': $pre .= $default_pre .= 'operate_move.htm'; break; case '404': $pre .= $default_pre .= '404.htm'; break; case 'read_404': $pre .= $default_pre .= 'read_404.htm'; break; case 'list_404': $pre .= $default_pre .= 'list_404.htm'; break; default: $pre .= $default_pre .= theme_mode_pre(); break; } if ($config['theme']) { $conffile = APP_PATH . 'view/template/' . $config['theme'] . '/conf.json'; $json = is_file($conffile) ? xn_json_decode(file_get_contents($conffile)) : array(); } !empty($json['installed']) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . ($id ? $id . '_' : '') . $pre; (empty($path_file) || !is_file($path_file)) and $path_file = APP_PATH . 'view/template/' . $config['theme'] . '/htm/' . $pre; if (!empty($config['theme_child']) && is_array($config['theme_child'])) { foreach ($config['theme_child'] as $theme) { if (empty($theme) || is_array($theme)) continue; $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . ($id ? $id . '_' : '') . $pre; !is_file($path_file) and $path_file = APP_PATH . 'view/template/' . $theme . '/htm/' . $pre; } } !is_file($path_file) and $path_file = APP_PATH . ($dir ? 'plugin/' . $dir . '/view/htm/' : 'view/htm/') . $default_pre; return $path_file; } function theme_mode_pre($type = 0) { global $config; $mode = $config['setting']['website_mode']; $pre = ''; if (1 == $mode) { $pre .= 2 == $type ? 'portal_category.htm' : 'portal.htm'; } elseif (2 == $mode) { $pre .= 2 == $type ? 'flat_category.htm' : 'flat.htm'; } else { $pre .= 2 == $type ? 'index_category.htm' : 'index.htm'; } return $pre; } ?>javascript - Error when using angular.copy() to copy data returned from ngResource - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Error when using angular.copy() to copy data returned from ngResource - Stack Overflow

programmeradmin37浏览0评论

I have the following code:

getContents: function ($scope, entityType, action, subjectId, contentTypeId, contentStatusId) {
                entityService.getContents(entityType, action, subjectId, contentTypeId, contentStatusId)
                .then(function (result) {
                    $scope.grid.data = result;
                    angular.copy($scope.grid.data, $scope.grid.originalData);
                    $scope.grid.newButtonEnabled = true;
                }, function (result) {
                    alert("Error: No data returned");
                    $scope.grid.newButtonEnabled = false;
                });
            },

and then the following function in entityService:

        getContents: function (entityType, action, subjectId, contentTypeId, contentStatusId) {
            var deferred = $q.defer();
            EntityResource.getEntities({ entityType: entityType, subjectId: subjectId, contentTypeId: contentTypeId, contentStatusId: contentStatusId },
               function (resp) {
                   deferred.resolve(resp);
               }
            );
            return deferred.promise;
        },

When I try to do an angular.copy I get a message saying:

TypeError: Object #<Object> has no method 'push'
    at Object.copy (http://127.0.0.1:81/Scripts/angular.js:600:21)
    at factory.getContents.entityService.getContents.then.$scope.grid.newButtonEnabled (http://127.0.0.1:81/Content/app/admin/services/grid-service.js:303:29)
    at deferred.promise.then.wrappedCallback (http://127.0.0.1:81/Scripts/angular.js:7303:59)
    at ref.then (http://127.0.0.1:81/Scripts/angular.js:7340:26)
    at Object.$get.Scope.$eval (http://127.0.0.1:81/Scripts/angular.js:8685:28)
    at Object.$get.Scope.$digest (http://127.0.0.1:81/Scripts/angular.js:8548:23)
    at Object.$get.Scope.$apply (http://127.0.0.1:81/Scripts/angular.js:8771:24)
    at done (http://127.0.0.1:81/Scripts/angular.js:10004:20)
    at pleteRequest (http://127.0.0.1:81/Scripts/angular.js:10180:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://127.0.0.1:81/Scripts/angular.js:10144:11) 

Does anyone have an idea of how I can make a copy of the data returned. Note that this data es back from ngResource. Also this seems different from the other problems that I find on Stackoverflow related to: TypeError: Object # has no method 'push' questions. With this problem I am getting the data back okay. It goes into $scope.grid.data but then gives me an error when trying to angular.copy the data.

I have the following code:

getContents: function ($scope, entityType, action, subjectId, contentTypeId, contentStatusId) {
                entityService.getContents(entityType, action, subjectId, contentTypeId, contentStatusId)
                .then(function (result) {
                    $scope.grid.data = result;
                    angular.copy($scope.grid.data, $scope.grid.originalData);
                    $scope.grid.newButtonEnabled = true;
                }, function (result) {
                    alert("Error: No data returned");
                    $scope.grid.newButtonEnabled = false;
                });
            },

and then the following function in entityService:

        getContents: function (entityType, action, subjectId, contentTypeId, contentStatusId) {
            var deferred = $q.defer();
            EntityResource.getEntities({ entityType: entityType, subjectId: subjectId, contentTypeId: contentTypeId, contentStatusId: contentStatusId },
               function (resp) {
                   deferred.resolve(resp);
               }
            );
            return deferred.promise;
        },

When I try to do an angular.copy I get a message saying:

TypeError: Object #<Object> has no method 'push'
    at Object.copy (http://127.0.0.1:81/Scripts/angular.js:600:21)
    at factory.getContents.entityService.getContents.then.$scope.grid.newButtonEnabled (http://127.0.0.1:81/Content/app/admin/services/grid-service.js:303:29)
    at deferred.promise.then.wrappedCallback (http://127.0.0.1:81/Scripts/angular.js:7303:59)
    at ref.then (http://127.0.0.1:81/Scripts/angular.js:7340:26)
    at Object.$get.Scope.$eval (http://127.0.0.1:81/Scripts/angular.js:8685:28)
    at Object.$get.Scope.$digest (http://127.0.0.1:81/Scripts/angular.js:8548:23)
    at Object.$get.Scope.$apply (http://127.0.0.1:81/Scripts/angular.js:8771:24)
    at done (http://127.0.0.1:81/Scripts/angular.js:10004:20)
    at pleteRequest (http://127.0.0.1:81/Scripts/angular.js:10180:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://127.0.0.1:81/Scripts/angular.js:10144:11) 

Does anyone have an idea of how I can make a copy of the data returned. Note that this data es back from ngResource. Also this seems different from the other problems that I find on Stackoverflow related to: TypeError: Object # has no method 'push' questions. With this problem I am getting the data back okay. It goes into $scope.grid.data but then gives me an error when trying to angular.copy the data.

Share Improve this question edited Jul 22, 2013 at 9:36 asked Jul 22, 2013 at 9:29 user1943020user1943020
Add a ment  | 

1 Answer 1

Reset to default 9

Take a close look at angular.copy doc:

destination(optional) – {(Object|Array)=} – Destination into which the source is copied. If provided, must be of the same type as source.

The result you got back from service call is probably an array; you however initialized $scope.grid.originalData as an object or something else other than array, so you got this type error.

Try to figure out what type the result is, and make $scope.grid.originalData the same type before calling angular.copy.

发布评论

评论列表(0)

  1. 暂无评论