我的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将我的数组元素的某些值设置为null ???
P.S。我在控制器中使用由此检索的数据,将其分配给范围变量,并在表单上执行ng-repeat。
编辑: 我的控制器代码如下
$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">
答案 0 :(得分:0)
编辑指令模板代码如下
<input class="form-control"
ng-model="ngModel" ng-init="ngModel">
您的代码将ng-model设置为null(ng-init = "ngModel = null
),因为您有两种方式与控制器绑定,此初始化为null将影响控制器范围对象。