控制器函数中的$ scope.data未定义(ANGULARJS)

时间:2014-08-27 03:28:38

标签: javascript angularjs angularjs-scope

我有一个函数通过POST到api来创建一个组,但现在我试图让这个函数将新创建的组附加到用户feed而不需要刷新页面。我知道我必须做一个.push并将数据推送到数组,但我有一个问题,在.success(函数(数据){} $ scope.data回来未定义所以我无法将数据推送到$ scope.feeds数组。有什么建议吗?

hello.controller('FeedListCtrl', ['$scope', '$http', '$window', '$cookies', '$modal', '$log', 'FeedService', 
function FeedListCtrl($scope, $http, $window, $cookies, $modal, $log, FeedService, $modalInstance) {

    $(function() {
        $('.ns-notify').css("visibility", "visible");
        $('body').css("padding", "40px 0");
    });     

$scope.feeds = [];


    $scope.createGroupCall = function createGroupCall(teacher, name) {
            $modalInstance.close();
            FeedService.createGroupCall($cookies.token, $window.sessionStorage.user, teacher, name).success(function(data) {

            console.log('GroupCreated');
            console.log(data.name);
            console.log(data.teacher);
            console.log($scope.create);
            console.log($scope.data + "THIS IS SCOPE.DATA");
            console.log($scope.feeds + 'HEY!');
            $scope.feeds.push($scope.data);
            $scope.feedCall($cookies.token);
        }).error (function(status,data,token) {
            console.log(status);
            console.log(data);
        });

};

$scope.feedCall = function feedCall(token) {
    FeedService.feedCall(token).success(function(data) {
        $scope.feeds = data.results;

        console.log("FEED Controller")
        // console.log(data);
        console.log(data.results);
        console.log($scope.feeds);
    }).error (function(status,data,token) {
        console.log(status);
        console.log(data);
    });
}

$scope.feedCall($cookies.token);
}]);


<div>
    <script type="text/ng-template" id="CreateGroupContent.html">
        <div class="modal-header">
                <h2 class="modal-title ns-modal-title">Create A Group</h2>
                <button class="ns-modal-close" ng-click="cancel()"><img src="images/xCancel.png"></button>
            </div>
            <div class="modal-body">
                <form class="form-signin" role="form">
                    <input type="text" class="form-control ns-modal-form" placeholder="Teacher" ng-model="create.teacher" required autofocus>
                    <input type="text" class="form-control ns-modal-form" placeholder="Group Name" ng-model="create.name" required>
                    <button class="btn btn-primary ns-modal-add" ng-click="createGroupCall(create.teacher, create.name);" type="submit">Create</button>
                </form>
            </div>
            <div class="modal-footer">
            </div>
        </div>
    </script>
</div>

我正在使用bootstraps angular-ui插件为我的模态解释$ modal,$ modalInstance和$ log

即使$ scope.data回来未定义,如果我控制台.log(数据)我实际上得到了数据。

0 个答案:

没有答案