Ashok Reddy Ashok Reddy - 1 year ago 125
Javascript Question

Unable to bind data to dropdown-multiselect in angularjs

Please check the plunker. I'm not able to bind the data which come from server like this


I know data binds when it is like

$scope.selectedUser = [{ id: 2, name: 'Monday' },
{id: 3,name:'Tuesday'}];.

I want to make
[{ id: 2, name: 'Monday' },{id: 3,name:'Tuesday'}]
in javascript so that it would bind in dropdown.

Please help me solve this issue.

demo plunker

Answer Source
//This is the ng filter to create id as you get the data from server 
    return function(arr){
        var result = [];
        for(var i = 0;i < arr.length;i++){
            var id = arr[i].substring(0,3);
            var obj = {'id':id,'name':arr[i]};
        return result;

//In js controller you can costomize your array of user by using ng-filter like this
$scope.users = $filter('createId')($scope.users);
//This is because index will be changed every time so this can't be used as ID
$scope.selectedUser = $filter('createId')($scope.selectedUser);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download