Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取页面刷新时的默认选定项_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 获取页面刷新时的默认选定项

Javascript 获取页面刷新时的默认选定项,javascript,html,angularjs,Javascript,Html,Angularjs,我有一个建立在平均堆栈上的网站,我有一个选择框,其中包含一些从MongoDB检索的城市名称。现在我使用Angular在HTML中显示这些名称,但现在我希望在加载页面时显示默认名称,以便至少显示一些信息。我已经搜索了我可以使用的内容,有人建议使用ng init,但这对我不起作用。还有其他选择吗 这是我的HTML: <div ng-controller="cityCtrl"> <div class="container plumbers"> <div cla

我有一个建立在平均堆栈上的网站,我有一个选择框,其中包含一些从MongoDB检索的城市名称。现在我使用Angular在HTML中显示这些名称,但现在我希望在加载页面时显示默认名称,以便至少显示一些信息。我已经搜索了我可以使用的内容,有人建议使用ng init,但这对我不起作用。还有其他选择吗

这是我的HTML:

<div ng-controller="cityCtrl">
  <div class="container plumbers">
    <div class="searchbox">
       <div class="plumber-by-city col-sm-12" style="margin-bottom: 20px;">
       <div class="title-1">Zoek op plaatsnaam</div>
          <select ng-model="selectedItem" ng-options="item as item.city for item in items"></select><span class="fa fa-caret-down"></span>
       </div>
    </div>
  </div>
</div>

您可以使用
ng selected
|
运算符,这样,如果
selectedItem
有一个值,则将选中该值,否则默认情况下将选中数组的第一个对象

var-app=angular.module(“MyApp”,[]).controller(“cityCtrl”,function($scope){
$scope.items=[{city:'abc'},{city:'cde'},{city:'fgh'}];
});

普拉特斯纳姆酒店
{{selectedItem}}

您应该初始化
选择editem
。试试这个
$scope.selectedItem=$scope.items[0]
除了哈迪的答案之外,你还可以初始化
$scope.items=[{city:amsterdam}]
或者不管你的回答看起来如何,哪个值应该显示为默认值
selectedItem
?Thanx Hadi,这就成功了:)
app.controller('cityCtrl', function($scope,$http){
  $scope.items = [];

  $http.get('/getdata').then(function(d){
    console.log(d);
    $scope.items = d.data;
  },function(err){
    console.log(err);
  });
});