Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.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 当数据来自API时,如何设置所选的特定值_Javascript_Angularjs - Fatal编程技术网

Javascript 当数据来自API时,如何设置所选的特定值

Javascript 当数据来自API时,如何设置所选的特定值,javascript,angularjs,Javascript,Angularjs,我有一个下拉列表:- <label>User Code:</label> <select ng-options="c as c.User for c in userList" ng-model="selectedUser" id="search3"> </select> 有一个用户代码:-SS1234。我希望在页面加载时选择此用户。我正在考虑使用selected属性,但不确定如何在获取的数据上使用它。在控制器中添加 $scope.sele

我有一个下拉列表:-

<label>User Code:</label>
<select ng-options="c as c.User for c in userList"
    ng-model="selectedUser" id="search3">
</select>

有一个用户代码:-SS1234。我希望在页面加载时选择此用户。我正在考虑使用
selected
属性,但不确定如何在获取的数据上使用它。

在控制器中添加

$scope.selectedUser = 'SS1234';

在控制器中添加

$scope.selectedUser = 'SS1234';

使用
ng init
ng value

<select ng-options="c as c.User for c in userList" ng-model="selectedUser" 
 id="search3" ng-value={{selectedUser = 'SS1234'}}></select>

//或


使用
ng init
ng value

<select ng-options="c as c.User for c in userList" ng-model="selectedUser" 
 id="search3" ng-value={{selectedUser = 'SS1234'}}></select>

//或


只需指定,
范围。选择所需的用户

scope.userList = response.data;
scope.selectedUser = scope.userList[0].User /* here i choose 0th index of user. you can change as you want */

只需登录,
scope.selectedUser=“SS1234”
您想要的用户

scope.userList = response.data;
scope.selectedUser = scope.userList[0].User /* here i choose 0th index of user. you can change as you want */

scope.userList数据是什么?您不需要使用
selected
属性,只需将
$scope.selectedUser
设置为与列表中所需的项目相等即可。scope.userList数据可能与scope.userList数据重复?您不需要使用
selected
属性,只需将
$scope.selectedUser
设置为与列表中所需的项目相等可能的重复项。这不是从返回的承诺中选择第一个选项吗?这就是我提到使用
ng init
的原因。这不是从返回的承诺中选择第一个选项吗?这就是我提到使用
ng init