Angularjs 角度,尝试在ng中显示对象重复失败
我正在用javascript编写一个带有angularJS和ionicframework(上一个beta v.11)的移动应用程序,我创建了一个动态对象,并希望以ng repeat的形式显示其中的所有对象。为什么不显示任何内容? 这是我的对象的屏幕: 我使用此代码将值放入范围:Angularjs 角度,尝试在ng中显示对象重复失败,angularjs,object,ng-repeat,Angularjs,Object,Ng Repeat,我正在用javascript编写一个带有angularJS和ionicframework(上一个beta v.11)的移动应用程序,我创建了一个动态对象,并希望以ng repeat的形式显示其中的所有对象。为什么不显示任何内容? 这是我的对象的屏幕: 我使用此代码将值放入范围: $scope.distanceSuppliers = myCar; 这是html中的代码: <ion-item ng-repeat="(id, supplier) in distanceSuppliers"&g
$scope.distanceSuppliers = myCar;
这是html中的代码:
<ion-item ng-repeat="(id, supplier) in distanceSuppliers">
<div class="items item-button-right" ng-click="openDetails(id)">
{{supplier.name}}<br />
{{supplier.address}}<br />
</div>
</ion-item>
怎么了?不确定,但我相信您有数据绑定更新问题 尝试使用$timeout强制渲染:
w_position.getPosition(function (error, position) {
$timeout(function() {
suppliers.on('value', function(supp) {
$scope.distanceSuppliers = {};
tot = 0;
done = 0;
supp.forEach(function(childSnapshot) {
tot++;
var childData = childSnapshot.val();
if (childData.address) {
calculateDistance(childData, position.coords.latitude, position.coords.longitude);
}
});
});
$ionicLoading.hide();
});
});
不要忘记将$timeout
参数添加到控制器:
.controller('suppliers', function($scope, ...<other parameters here>..., $timeout) {
.controller('suppliers',函数($scope,…,$timeout){
我发现了问题!使用$scope.$apply();
问题是我使用以下代码在不同的$scope中编写:
cw_position.getPosition(function (error, position) {
suppliers.on('value', function(supp) {
tot = 0;
done = 0;
supp.forEach(function(childSnapshot) {
tot++;
var childData = childSnapshot.val();
if (childData.address) {
calculateDistance(childData, position.coords.latitude, position.coords.longitude);
}
});
});
$ionicLoading.hide();
});
angular.module('cw_position', [])
.service('cw_position', function() {
this.getPosition = function(callback) {
navigator.geolocation.getCurrentPosition(
function (position) {
return callback(null, position);
},
function(error) {
return callback(error, null);
}
);
}
// Built google maps map option
//
this.getGoogleMapOptions = function (lat, lon, zoom, type) {
if (type == null) {
type = google.maps.MapTypeId.ROADMAP;
}
var mapOptions = {
center: new google.maps.LatLng(lat, lon),
zoom: zoom,
mapTypeId: type
};
return mapOptions;
}
});
其中cw_position.getPosition使用以下代码调用js:
cw_position.getPosition(function (error, position) {
suppliers.on('value', function(supp) {
tot = 0;
done = 0;
supp.forEach(function(childSnapshot) {
tot++;
var childData = childSnapshot.val();
if (childData.address) {
calculateDistance(childData, position.coords.latitude, position.coords.longitude);
}
});
});
$ionicLoading.hide();
});
angular.module('cw_position', [])
.service('cw_position', function() {
this.getPosition = function(callback) {
navigator.geolocation.getCurrentPosition(
function (position) {
return callback(null, position);
},
function(error) {
return callback(error, null);
}
);
}
// Built google maps map option
//
this.getGoogleMapOptions = function (lat, lon, zoom, type) {
if (type == null) {
type = google.maps.MapTypeId.ROADMAP;
}
var mapOptions = {
center: new google.maps.LatLng(lat, lon),
zoom: zoom,
mapTypeId: type
};
return mapOptions;
}
});
navigator.geolocation.getCurrentPosition导致“问题”
谢谢大家的帮助你能提供一个重现问题的JSFIDLE或PLUNK吗?我没有看到任何错误,我用你的代码创建了这个PLUNK,如果它在正确的$rootscope中,它工作得很好。这是JSFIDLE的链接:@SimoneM我已经为你提供了一个工作示例。你的小提琴不工作,因为它是错误的y配置。如果您需要进行任何更改,请使用我的。thx,我尝试过,但我认为这不是超时问题,当我尝试在console distanceSuppliers中显示它工作时,我认为这意味着所有代码都被执行
$timeout
执行完全相同的操作…请阅读。