angucomplete-alt选择对象数据

时间:2017-10-13 15:10:30

标签: angularjs

文档中的

表示:

selected-object-data - >第二个参数将传递给selected-object。我试图使用它,但回调函数没有收到它。 我做错了什么?

<div data-angucomplete-alt=""
   id="agent"
   data-placeholder="Type to search"
   data-pause="400"
   data-selected-object="callbackFunction"
   data-selected-object-data="row"
   data-remote-url="getClients?searchString="
   data-remote-url-data-field="Clients"
   data-title-field="CompanyName"
   data-input-class="form-control"
   data-match-class="highlight"
   data-minlength="2"
   data-initial-value="{{row.agentCompany}}"
   data-remote-url-response-formatter="formatAutoCompleteJson">
</div>


$scope.callbackFunction = function (selected) {
    console.log(selected);  //print only the selected object, no the data (second parameter)
    //how to get the second parameter?
}

4 个答案:

答案 0 :(得分:1)

我认为如果您在回调函数中添加了第二个参数,那么您可以访问“row”的值

$scope.callbackFunction = function (selected, row) {
    console.log(selected);  
    console.log(row); // should print the row number
}

答案 1 :(得分:0)

我在控制器中创建了一个初始化函数,它将回调函数返回到repeat的范围内。

<html>
<div ng-repeat="row in rows">
...
<div ng-init="row.callbackFunction = initialiseCallback(row)"></div>
<angucomplete-alt selected-object = "row.callbackFunction" />
...
</div>
</html>

Controller() {
    $scope.initialiseCallback = function (row){
        return function function (selected) {
            console.log(selected);  
            console.log(row); // should print the row number
        }
    }
}

答案 2 :(得分:0)

我刚刚发现我使用的是angucomplete的旧版本,将其更新为最新版本,并且该参数开始起作用。

答案 3 :(得分:0)

如果您使用的是ng-repeat 例如

<ul>
<li ng-repeat="customer in customers track by $index">
                    <angucomplete-alt pause="500"
                                      selected-object="selectedCustomer"
                                      selected-object-data="$index"
                                      remote-url={{apiURL}}/customers/q?search="
                                      remote-url-data-field="customers"
                                      title-field="surname,name"
                                      description-field="address"
                                      minlength="3"/>
</li>
</ul>

如果在选定对象日期中绑定了ng-repeat的$ index 在这种情况下,您可能会遇到这种情况

  $scope.selectedCustomer = function(customer, index) {
    console.log(customer, index);
  };