簡體   English   中英

Angular-UI Bootstrap Typeahead,多個輸出?

[英]Angular-UI Bootstrap Typeahead, multiple output?

所以我正在使用Typeahead從json文件中獲取一些信息。 可以正常工作,沒問題。 但是,當我在第一個輸入對象中選擇一個對象時,我想“自動完成”其他一些輸入字段。

假設第一個字段如下所示:

<input type="text" class="form-control" id="supertag" ng-model="selected" uib-typeahead="title.title for title in tags | filter:$viewValue | limitTo:8"/>

它在該輸入字段中顯示title.title,太好了! 我已經嘗試了幾種方法來解決這個難題,但是當第一個選擇並准備好時,我似乎無法填充其余的輸入字段。 我想在下一個目錄中添加諸如title.subtitle之類的內容,但這似乎並不那么簡單。

我想念什么? 例如,這不起作用:

<input type="text" class="form-control" id="rubrik" value="{{title.subtitle}}"/>

謝謝!

編輯:JSON看起來像這樣:

{
 title: 'title',
 subtitle: 'subtitle',
 id: 'id'
},
{
 title: 'title',
 subtitle: 'subtitle',
 id: 'id'
},

編輯:進一步的代碼,用於不同json文件之間的比較。

這就是輸入字段所指向的內容:

$http.get(tagUrl)
        .then(function(res) {
            $scope.tags = res.data;
        })

然后onSelect:

$scope.onSelect = function($item, $model, $label) {
        $scope.id = $item.id;
        $scope.title = $item.title;
        $scope.selected = $item.selected;
        $scope.subtitle = $item.subtitle; <- undefined because it only exists in JSON2, not in JSON1.
        console.log($scope.id);
    };

這是我希望函數對輸入ID進行比較的地方。 它將始終存在於JSON1(tagUrl)處,但我還希望它在繼續進行操作之前查看它是否存在於JSON2(superUrl)中(並且如果存在,請將$ scope.subtitle = $ item.subtitle設置為相應的輸入字段) 。 我使用if語句嘗試了不同的方法,但最好的情況是我不確定。

我有一個ng-repeat,它列出了頁面上的所有superUrl,這對於獲取我想要的內容可能很有用。

$http.get(listSuperUrl)
        .then(function(res) {
            $scope.current = res.data;
        })

您需要typeahead-on-select="vm.onSelectItem($item, $model, $label)輸入元素中添加typeahead-on-select="vm.onSelectItem($item, $model, $label) (如果您將控制器用作vm)或typeahead-on-select="onSelectItem($item, $model, $label) (如果在控制器中使用$ scope)。 您可以隨意命名onSelectItem,這是我用於說明的函數。

<input type="text" ng-model="subtitle" class="form-control" id="rubrik"/>

在您的視圖中完成此操作后,轉到控制器,定義:

vm.onSelectItem = function($item, $model, $label){
  /*bind your subtitle ngModel to $item.subtitle*/
  vm.subtitle = $item.subtitle;
}

要么

$scope.onSelectItem = function($item, $model, $label){
 /*bind your subtitle ngModel to $item.subtitle*/
 $scope.subtitle = $item.subtitle;
}

這應該為您完成,如果我沒有在代碼中嘗試過,請告訴我是否可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM