Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.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 我在angular JS中选择的默认选项_Javascript_Html_Angularjs - Fatal编程技术网

Javascript 我在angular JS中选择的默认选项

Javascript 我在angular JS中选择的默认选项,javascript,html,angularjs,Javascript,Html,Angularjs,我正在使用AngularJS创建一个简单的web应用程序 我想在我的选择中显示城市A的值作为默认选项 index.html <body ng-controller="MainCtrl"> <select ng-model="selectedCity" ng-change="extractSubsities(selectedCity)" ng-options="item as item.name for item in cities track by item.name"&

我正在使用AngularJS创建一个简单的web应用程序

我想在我的选择中显示城市A的值作为默认选项

index.html

<body ng-controller="MainCtrl">
  <select ng-model="selectedCity"  ng-change="extractSubsities(selectedCity)" ng-options="item as item.name for item in cities track by item.name">
  </select>

  <select ng-show="selectedCity.subsities" ng-model="selectedSubCity"  ng-change="extractSubsities(selectedSubCity)" ng-options="item2 as item2.name for item2 in selectedCity.subsities track by item2.name">
   <option style="" value=""></option>
   </select>

  <table>
    <tr ng-repeat="item3 in data track by item3.id">
      <td>{{ item3.id }}</td>
      <td>{{ item3.name }}</td>
      <td>{{ item3.price }}</td>
    </tr>
  </table>

</body>

</html>

我需要将城市A显示为默认的选定值。请问如何解决此问题?

您可以通过将此行添加到控制器,在页面加载完成之前设置选定的城市

大概是这样的:

$scope.selectedCity = $scope.cities[0].name;

只需将
selectedSubCity
指定为默认值:

$scope.selectedSubCity = $scope.cities[0];
也可称为子公司:

$scope.extractSubsities();
您可以使用该指令初始化您的选择。您还必须调用
extractSubstities()
函数来填充表:

 <select ng-model="selectedCity"  ng-change="extractSubsities(selectedCity)" ng-options="item as item.name for item in cities track by item.name" ng-init="selectedCity = cities[0];extractSubsities(selectedCity)">
  </select>


这是一个正在工作的

看,我把它添加到了我的控制器中,它与我不起作用如果你可以在小提琴示例中使用它链接请注意你的脚本是正确的,但它只将城市a设置为默认选项,但它无法在我的表{item3.id}{{item3.name}{{item3.price}上打印它的值@Abderrahim你能检查一下这把小提琴吗,这是我们需要的吗@Abderrahim问题是,您可以在
ng init
中添加多个函数这是代码的重要部分
ng init=“selectedCity=cities[0];extractSubstities(selectedCity)”
@Abderrahim是我的好朋友,如果有任何不清楚的地方,请不要犹豫,询问非常感谢,我还有其他问题,如何在第二个选择中设置默认选项,例如选择sub A1作为默认选项如果与您合作,您可以在小提琴示例链接中进行设置,请
 <select ng-model="selectedCity"  ng-change="extractSubsities(selectedCity)" ng-options="item as item.name for item in cities track by item.name" ng-init="selectedCity = cities[0];extractSubsities(selectedCity)">
  </select>