下拉值不会更改/设置为默认值

时间:2017-10-06 10:47:52

标签: javascript angularjs

我在角度js中创建了一个下拉列表。

参考下面的代码:

<select name="artist" class="form-control selectpicker" ng-options="artists.Title for artists in artists track by artists.Id" ng-model="artist.Title">
    <option value="">Select Artist</option>
</select>

这里我使用REST API从后端获得artists值。在成功回调后,我调用另一个函数,即getSongs()来获取所选的项值。

var songIndex = $routeParams.index;
EditFactory.getArtists().then(getArtistsSuccess, getArtistsFailed);

function getArtistsSuccess(data) {
    if (data.data.d.results.length > 0) {
        $scope.artists = data.data.d.results;
    }
    ListFactory.getSongs().then(getSongsSuccess, getSongsFailed);
}

function getArtistsFailed(error) {
    console.log("empty artists from server -- addController");
}

function getSongsSuccess(data) {

    var currentSongObj = data.data.d.results.filter(function(obj) {
        return obj.ID == songIndex;
    });

    $scope.listItems = data.data.d.results;
    $scope.artist = currentSongObj[0].Artist; // need to update this value in view

}

function getSongsFailed(error) {
    debugger;
}

此处artist值未在下拉列表中设置。

请参阅下面的屏幕截图。

enter image description here

当我在$scope.artist.Title发出警报时,我获得了正确的价值。我无法找到我做错的事。

请建议我..

感谢。

1 个答案:

答案 0 :(得分:0)

你的代码中有很多错误,可能是拼写错误,反正你应该写select

<select name="artist" class="form-control selectpicker" 
      ng-options="artist as artist.Title for artist in artists  track by artist.Id" 
      ng-model="selectedArtist">
    <option value="">Select Artist</option>
</select>

selectedArtist

$scope.selectedArtist = {
    Id:2
};

Demo Fiddle