2015-09-27 54 views
1

我尝试了很多,但没有找到能够解决我的问题,正确的答案。希望有人会帮助我。阿贾克斯数据ngView更新后没有反映过ngRoute :(

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时调用视图的路线,我从AJAX调用的viewAction和装载数据,但新的数据并不在视图中得到更新仍然存在的帮助。

请帮助!

+0

在默认情况下,您只使用templateUrl作为list.html,但在路由器配置中没有控制器初始化。你确定当你的list.html被加载时你的MainContoller的indexAction被调用了吗? – Arkantos

回答

0

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

1

你的location.path看起来像$ location.path('new'),当它们看起来像$ location.path('/ new');当它看起来像$ location.path('/ view'+ dataId)时,你的另一个看起来像$ location.path('view /'+ dataId);

+0

但@rethabile模板得到加载,我可以看到UI更改。如果我的$ location.path('new')应该是$ location.path('/ new'),那么在前面的例子中,模板不应该加载?如果我错了,请告诉我。 (对不起,延迟发表评论) – Code

+0

你可以请回答 – Code