2015-09-27 53 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