2016-10-28 29 views
0

我有一個factory其中我設置一個IP是所有控制器通用的。我得到在其他控制器的IP,並添加像這樣的Web服務方法:url : $scope.ipForHttp+"getDeviceDetailsReport"其中$scope.ipForHttphttp://websitename.com/我從factory得到。獲取未定義的東西,我沒有聲明

這適用於我的本地計算機。當我主持這個並打開頁面時,我首次爲所有頁面獲取它。但是當我刷新頁面時,我在控制檯中看到一個錯誤。

GET http://websitename.com/undefinedgetDeviceDetailsReport 404 (Not Found)。我只在刷新頁面時纔會出現此錯誤,並且僅在託管網站中,而不在本地計算機中。

我在/和方法名getDeviceDetailsReport之間得到undefined

這是一些地方我使用factory數據代碼:

$scope.ipForHttp = userDetailsFactory.getUserDetailsFromFactory().ipAddress; 
$scope.loading = false; 
$scope.ClientID = userDetailsFactory.getUserDetailsFromFactory().clientidFromSession; 
// $scope.dev={}; 
$scope.getDevice =function(){ 
    $scope.loading = true; 
     $http({ 
     method : "GET", 
     url : $scope.ipForHttp+"getDeviceDetailsReport" //i think undefined error here. 

而且我得到這個Error: ngRepeat:dupes Duplicate Key in Repeater也只有當我刷新頁面。

編輯。我運行factory並在每次頁面刷新時設置該值。也許第二次的價值是undefined ..但我不知道我還有沒有舊的價值。

也許這就是爲什麼我刷新時得到Error: ngRepeat:dupes。我究竟做錯了什麼?

索引頁的(包含其他HTML頁)控制器與factory

angular.module('tollApp') 
.controller('indexController', function($scope,$http,$window,userDetailsFactory){ 
    $scope.usernameFromServer={}; 
    $scope.getUserDetails = function(){ 
     $http({ 
      method:'GET', 
      url:'http://192.168.1.80:4000/getUserDetails' 
      // url:'http://websitename.com/getUserDetails' 
     }) 
     .then(function(response){ 

      // console.log(JSON.stringify(response)); 
      userDetailsFactory.setUserDetailsInFactory(response.data); 
     $scope.usernameFromFactory = userDetailsFactory.getUserDetailsFromFactory().usernameFromSession; 
     $scope.theIP = userDetailsFactory.getUserDetailsFromFactory().ipAddress; 
      // $scope.usernameFromServer = userDetailsFactory.getUserDetailsFromFactory().username; 
      // console.log(JSON.stringify($scope.usernameFromFactory)+"usernameFromFactory"); 
     }) 
    } 
    $scope.logout = function(request,response){ 
     $http({ 
      method:'GET', 
      url:'/logout' 
     }) 
     .then(function(response){ 
      console.log(JSON.stringify(response)); 
      if(response.data=="logout"){ 
       // $window.location.href="http://websitename.comlogin"; 
       $window.location.href="http://192.168.1.80:4000/login"; 

      } 
     }) 
    } 
    console.log("indexController"); 
}).factory('userDetailsFactory',function(){ 
    var user = {}; 
    return { 
     setUserDetailsInFactory : function(val){ 
      user.useridFromSession = val[0].UserID; 
      user.usernameFromSession = val[0].UserName; 
      user.userroleFromSession = val[0].UserRole; 
      user.clientidFromSession = val[0].ClientID; 
      user.ipAddress = "http://192.168.1.80:4000/"; 
      // user.ipAddress = "http://websitename.com/"; 
      // console.log("in set "+user.clientidFromSession); 
     }, 
     getUserDetailsFromFactory : function(){ 
      return user; 
     } 
    }; 
}); 
+0

你正在使用ng-repeat,它有一個重複鍵,使用'track by $ index'來首先解析它。 – Sravan

+0

@Sravan。第一個問題呢。另請參閱編輯 – Abhi

+0

您可能會附加該值,而不是替換它,請添加工廠代碼。 – Sravan

回答

0

該錯誤是因爲你正在做的與重複集合的NG-重複。當你對含有重複項的集合進行ng-repeat時,你需要按索引進行跟蹤。

+0

好的謝謝。但第一個問題呢 – Abhi

相關問題