2016-04-07 10 views
3

私はこのコンポーネントを使用しようとしています https://github.com/alexklibisz/angular-dual-multiselect-directive しかし、どのようにバックエンドからコンポーネントにデータを渡すことができるのか分かりません。別の変数の変数を角度で使用しますか?

Iは、バックエンドからデータを取得:

var vm = this; 
vm.Categoria1 = []; 
$http.get('http://localhost:5541/api/date/ListCategorii').success(function (data) { 
    vm.Categoria1 = data; 
} 
); 

私は、データを取得する:

[{"Id":1,"NumeCategorie":"Fructe"},{"Id":2,"NumeCategorie":"Legume"},{"Id":3,"NumeCategorie":"Ciocolata"}] 

しかしvm.Categoria1が空である以下:

$scope.demoOptions = { 
    title: 'Demo: Recent World Cup Winners', 
    filterPlaceHolder: 'Start typing to filter the lists below.', 
    labelAll: 'All Items', 
    labelSelected: 'Selected Items', 
    helpMessage: ' Click items to transfer them between fields.', 
    /* angular will use this to filter your lists */ 
    orderProperty: 'name', 
    /* this contains the initial list of all items (i.e. the left side) */ 
    items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }], 
    /* this list should be initialized as empty or with any pre-selected items */ 
    selectedItems: [] 
}; 

ありがとうございました。

+0

、あなたはすでにvm.Categoria1セットを持っていますか? $ http呼び出しは非同期であるため、demoOptionsオブジェクトを初期化した後にのみ結果を受け取ることができます。 –

答えて

1

のベストプラクティスは次のように使用することです:

var vm = this; 
vm.Categoria1 = []; 

$http.get('http://localhost:5541/api/date/ListCategorii') 
.success(function(data) { 
    vm.Categoria1 = data; 
    $scope.demoOptions = { 
    title: 'Demo: Recent World Cup Winners', 
    filterPlaceHolder: 'Start typing to filter the lists below.', 
    labelAll: 'All Items', 
    labelSelected: 'Selected Items', 
    helpMessage: ' Click items to transfer them between fields.', 
    /* angular will use this to filter your lists */ 
    orderProperty: 'name', 
    /* this contains the initial list of all items (i.e. the left side) */ 
    items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }], 
    /* this list should be initialized as empty or with any pre-selected items */ 
    selectedItems: [] 
}) 
.error(function(data, status) { 
    console.error('Repos error', status, data); 
}) 
.finally(function() { 
    console.log("finally finished repos"); 
}); 

希望に役立ちます!サミアイダカエイ問題。

1

あなたは成功コールバックでdemoOptionsを設定する必要があります。

var vm = this; 
vm.Categoria1 = []; 
$http.get('http://localhost:5541/api/date/ListCategorii').success(function (data) { 
     vm.Categoria1 = data; 
     $scope.demoOptions = { 
     title: 'Demo: Recent World Cup Winners', 
     filterPlaceHolder: 'Start typing to filter the lists below.', 
     labelAll: 'All Items', 
     labelSelected: 'Selected Items', 
     helpMessage: ' Click items to transfer them between fields.', 
     /* angular will use this to filter your lists */ 
     orderProperty: 'name', 
     /* this contains the initial list of all items (i.e. the left side) */ 
     items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }], 
     /* this list should be initialized as empty or with any pre-selected items */ 
     selectedItems: [] 
    }; 
}); 
0

あなたは要求成功の項目を設定する必要があります。

$http.get('http://localhost:5541/api/date/ListCategorii') 
    .success(function (data) { 
      $scope.demoOptions.items = vm.Categoria1 = data;    
     }); 
1

その他のオプションでは、1つの関数がそのhttp.get コールが含まれている作成し、ページのロードにその関数を呼び出すことができます。

あなたがdemoOptionsを設定した時刻に
var vm = this; 
vm.Categoria1 = []; 

var onload = function(){ 
    $http.get('http://localhost:5541/api/date/ListCategorii').success(function (data) { 
     vm.Categoria1 = data; 
    } 
}; 

$scope.demoOptions = { 
    title: 'Demo: Recent World Cup Winners', 
    filterPlaceHolder: 'Start typing to filter the lists below.', 
    labelAll: 'All Items', 
    labelSelected: 'Selected Items', 
    helpMessage: ' Click items to transfer them between fields.', 
    /* angular will use this to filter your lists */ 
    orderProperty: 'name', 
    /* this contains the initial list of all items (i.e. the left side) */ 
    items: vm.Categoria1, //[{ 'id': '50', 'name': 'Germany' }, { 'id': '45', 'name': 'Spain' }, { 'id': '66', 'name': 'Italy' }, { 'id': '30', 'name': 'Brazil' }, { 'id': '41', 'name': 'France' }, { 'id': '34', 'name': 'Argentina' }], 
    /* this list should be initialized as empty or with any pre-selected items */ 
    selectedItems: [] 
}; 

onload(); 
関連する問題