通过ngRoute的Ajax数据在通过ngRoute更新ngView之后没有反映:(

时间:2015-09-27 17:21:37

标签: angularjs

我尝试了很多,找不到可以解决问题的正确答案。希望有人能帮助我。

app.controller('MainController', ['$scope', 'MainService', 'CONSTANTS', '$routeParams', '$location',
            function($scope, MainService, CONSTANTS, $routeParams, $location) {

        $scope.indexAction = function() {
                    MainService.query({format: 'json'}, function(data){
                        $scope.data = data;
            **This data still there when viewAction get call.**
                    });
        }

                $scope.newAction = function($event) {
                    $scope.isNew = true;
                    angular.isDefined($event)? $event.preventDefault() : false;
                    if(angular.isDefined($event)) {
                        var postData = $('#form').serialize();
                        MainService.save({format: 'json'}, postData, function(data, responseHeader){
                            var loc = responseHeader('location');
                            var r = /\d+/;
                            var dataId = loc.match(r);
                            $scope.viewAction(dataId[0]);
                        });
                    }
                    else {
                        $location.path('new');
                    }
                }

                $scope.viewAction = function(ObjOrId) {
                    var dataId = null;
                    if(angular.isObject(ObjOrId)) {
                        dataId = ObjOrId.id;
                        $scope.data = ObjOrId;
                        $location.path('view/'+dataId);
                    }
                    else {
                        dataId = ObjOrId;
                        MainService.get({Id: ObjOrId, format: 'json'}, function(data) {
                            $scope.data = data;
                            $location.path('view/'+dataId);
                        });
                    }
                }


                $scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {

                });


        }
    ]);

    app.config(['$routeProvider', '$locationProvider',
        function($routeProvider, $locationProvider) {
            $routeProvider.
                when('/new', {
                    templateUrl: 'abc.html',
                    controller: 'MainController'
                }).
                when('/view/:Id', {
                    templateUrl: 'xyz.html',
                    controller: 'MainController'
                }).
                otherwise({
                    templateUrl: 'list.html'
                })
             $locationProvider.html5Mode({
                enabled: false
            });
        }
    ])

list.html中带有 indexAction 的数据,当查看路由调用时仍然存在,我调用viewAction并从ajax加载数据,但新数据未在图。

请帮助!!

2 个答案:

答案 0 :(得分:1)

你的location.path看起来像$ location.path('new'),当它们看起来像$ location.path('/ new');

你的另一个看起来像$ location.path('view /'+ dataId),看起来应该是$ location.path('/ view'+ dataId);

答案 1 :(得分:0)

我找到了答案,我在表单模板中使用ng-model并且更新$ scope.data对象而不提交表单本身,所以我将输入指令ng-model更改为ng-value并在迁移到视图时那里的模板我能够获得数据。