如何设置数据来自API时选择的特定值

时间:2017-06-28 09:20:37

标签: javascript angularjs

我有一个下拉列表: -

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

此下拉列表中的数据来自API。我的指令代码: -

scope.getAllPeriodicities = function(){
    scope.userList = [];
    ApiServices.getAllPeriodicities().then(function (response) {
        scope.userList = response.data;
    });
}

scope.getAllPeriodicities();

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

4 个答案:

答案 0 :(得分:1)

scope.getAllPeriodicities = function(){
  scope.userList = [];
    ApiServices.getAllPeriodicities().then(function (response) {
       scope.userList = response.data;
       scope.selectedUser = "SS1234";
   });
}
scope.getAllPeriodicities();

答案 1 :(得分:1)

使用ng-modelng-init

将您的默认值设为ng-value
<select ng-options="c as c.User for c in userList" ng-model="selectedUser" 
 id="search3" ng-value={{selectedUser = 'SS1234'}}></select>

//或

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

答案 2 :(得分:1)

只需asign,def complexNumberMultiply(self, a, b): """ :type a: str :type b: str :rtype: str """ c,d = a.split('+') e,f = b.split('+') d = int(d[:-1]) f = int(f[:-1]) print c,d,e,f return '%s+%si' % (c * e - d * f, c * f + d * e) 您想要的用户。

scope.selectedUser="SS1234"

答案 3 :(得分:0)

在你的控制器中添加

$scope.selectedUser = 'SS1234';