-2

我AngularJS服務代碼 -爲什麼AngularJS正在改變對象的值?

this.getEducation = function (id) { 
      var url = 'SOME_URL/?party_id=' + id; 
      var deferred = $q.defer(); 

      $http.get(url). 
       success(function (data, status, headers, config) { 
        console.log(data); 
        deferred.resolve(data); 
       }). 
       error(function (data, status, headers, config) { 
        console.log("could not get education info"); 
        deferred.reject(data); 
       }); 
      return deferred.promise; 
     } 

現在,我的服務返回一個數據是這樣 -

[ 
    { 
    "id": 22, 
    "party_id": 9, 
    ... 
    "university": "UoP", 
    "created_at": "2015-07-13 17:09:52", 
    "degree": "BE" 
    }, 
    { 
    "id": 23, 
    "party_id": 9, 
    ... 
    "university": "UoP", 
    "created_at": "2015-07-13 17:11:06", 
    "degree": "ME" 
    } 
] 

現在,這裏的問題 - 當data的承諾正在得到解決,包含以下陣列 -

[ 
     { 
     "id": 22, 
     "party_id": 9, 
     ... 
     "university": "UoP", 
     "created_at": "2015-07-13 17:09:52", 
     "degree": "BE" 
     }, 
     { 
     "id": 23, 
     "party_id": 9, 
     ... 
     "university": null, 
     "created_at": "2015-07-13 17:11:06", 
     "degree": null 
     } 
    ] 

所以,我的問題是,爲什麼AngularJS將我的數組元素的一些值設置爲空?

P.S.我在控制器中使用由此獲取的數據,將其分配給作用域變量,並對錶單執行ng-重複操作。

編輯: 我的控制器代碼如下

$scope.educationInformations = []; 
$scope.setEducation = function() { 
      EducationProvider.getEducation(id).then(
       function (educations) { 

        angular.forEach(educations, function(education){ 
         console.log(education); 
        $scope.educationInformations.push({education:education}); 
         console.log($scope.educationInformations); 
        }) 
      }); 
     }; 

這工作,(控制檯日誌是準確的)

現在,這是我的模板代碼。 當這個被使用,

<div ng-repeat="educationInfo in educationInformations"> 
<input-text ng-model="educationInfo.education.university"></input-text> 
</div> 

現在input-text是由我創造了一個指令.. 指令碼 -

.directive('inputText', function() { 
     return { 
      templateUrl: 'views/inputText.html', 
      restrict: 'E', 
      scope: { 
       ngModel: '=' 
      } 
     }; 
    }); 

指令模板

<input class="form-control" 
ng-model="ngModel" ng-init="ngModel = null"> 
+7

我很難相信角是這個原因。需要更多的調試。 –

+0

您是否通過檢查實際響應來查看從'$ http.get'請求返回的數據?它有'null'嗎? –

+0

是期望數據被驗證,還是隻是你想要的? –

回答

0

編輯指令模板代碼如下

<input class="form-control" 
ng-model="ngModel" ng-init="ngModel"> 

您的代碼是將ng-model設置爲null(ng-init = "ngModel = null),因爲您有兩種與控制器綁定的方式,此初始化爲null會影響控制器作用域對象。

相關問題