我正在MEAN堆栈上创建一个网站,我有一个模板可以在Angular中使用通配符路由。当用户键入plumber-in-“cityname”时,将检查cityname是否存在于带循环的DB中。如果存在,它将检索cityname附带的所有信息。但是,如果用户选择了数据库中不存在的城市,我现在只需要一个“找不到页面”页面。
我怎样才能实现这一目标?
这是我在控制器中的循环:
app.controller('DataCtrl2', function($scope,$http,getData,$location,$routeParams){
$scope.changePath = function(path) {
$location.path('/'+ path +'-' + $scope.selectedItem.city);
};
getData.getCity().then(function(response){
$scope.items = response.data;
var keepGoing = true;
for(var i = 0; i < $scope.items.length; i++){
var data = $scope.items[i];
if(keepGoing) {
if (angular.lowercase($routeParams.name) == angular.lowercase(data.city)) {
keepGoing = false;
$scope.selectedItem = data;
}
}
}
});
});
JSON:
{
"_id" : ObjectId("592409faf1f5831ca882ad39"),
"city" : "Aalsmeer",
"phoneLG" : "0101-010 001",
"phoneAV" : "0101-010 001",
"phoneCV" : "0101-010 001"
}
{
"_id" : ObjectId("592409faf1f5831ca882ad3a"),
"city" : "Aalburg",
"phoneLG" : "0101-010 001",
"phoneAV" : "0101-010 001",
"phoneCV" : "0101-010 001"
}