Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/417.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 没有页面刷新,无法更新$scope对象_Javascript_Angularjs_Controllers - Fatal编程技术网

Javascript 没有页面刷新,无法更新$scope对象

Javascript 没有页面刷新,无法更新$scope对象,javascript,angularjs,controllers,Javascript,Angularjs,Controllers,我使用Angular服务返回一个promise对象,然后添加到数据中,但是在没有页面刷新的情况下,视图不会使用新添加的元素进行更新 在没有页面刷新的情况下,如何在视图中刷新$scope.articles 视图: 编辑 添加了文章资源: 'use strict'; //Articles service used for communicating with the articles REST endpoints angular.module('articles').factory('Articl

我使用Angular服务返回一个promise对象,然后添加到数据中,但是在没有页面刷新的情况下,视图不会使用新添加的元素进行更新

在没有页面刷新的情况下,如何在视图中刷新$scope.articles

视图:

编辑

添加了文章资源:

'use strict';

//Articles service used for communicating with the articles REST endpoints
angular.module('articles').factory('Articles', ['$resource',
    function($resource) {
        return $resource('articles/:articleId', {
            articleId: '@_id'
        }, {
            update: {
                method: 'PUT'
            }
        });
    }
]);
此外,在节点中处理和响应请求的服务器控制器为:

exports.list = function(req, res) {
    Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            console.log(articles);
            res.json(articles);
        }
    });
};
这需要解决吗?如果有人能帮忙,我将不胜感激,因为我不知道如何使用解析

编辑:

我现在想知道这是否是一个路由问题,因为我有一个列表状态的子级创建状态-也许

function($stateProvider) {
        // Articles state routing
        $stateProvider.
        state('listArticles', {
            url: '/articles',
            templateUrl: 'modules/articles/views/list-articles.client.view.html' 

        }).
        state('listArticles.createArticle', {
            url: '/create',
            templateUrl: 'modules/articles/views/create-article.client.view.html'

谢谢

将查找功能更改为此

$scope.find = function() {
    Articles.query({}, function (response) {
        // Will update scope, function will be called if http status === 200
        $scope.articles = response;
    }, function () {
        // This is a error function will called if http status != 200
    });
};
查询函数不会返回任何内容,您需要在回调中设置数据,这样您可以将两个函数传递为success和error,或者将一个promise用于两个函数:

$scope.find = function() {
    Articles.query().$then(function (response) {
        // Will update scope, function will be called if http status === 200
        $scope.articles = response;
    }, function () {
        // This is a error function will called if http status != 200
    });
};

数据是在请求完成时设置的,因此范围将被更新

什么是
文章
-它是否适合摘要周期?文章是一个$resource服务请显示该代码。你不应该构建它的代码-有点麻烦-你能详细说明一下吗?嗨,Leo-尝试过这个,但仍然不起作用-这非常令人沮丧,你能用你的代码创建一个plunkr或codepen吗?也许你的资源错了,我需要调试:(
$scope.find = function() {
    Articles.query({}, function (response) {
        // Will update scope, function will be called if http status === 200
        $scope.articles = response;
    }, function () {
        // This is a error function will called if http status != 200
    });
};
$scope.find = function() {
    Articles.query().$then(function (response) {
        // Will update scope, function will be called if http status === 200
        $scope.articles = response;
    }, function () {
        // This is a error function will called if http status != 200
    });
};