2016-08-08 10 views
0

Ich treffe Datenbank auf Dropdown-On-Change-Ereignis. Aber ich muss Tags nach dem Benutzer machen, wählt einen von ihnen aus.Kann angularjs Funktion nicht aus Dropdown aufrufen (select) mit select2

jsp Datei

<select id="multipleSelectLocation" data-placeholder="Select an option" ng-model="search" ng-change="searchlocation(search)" multiple="true" > 
        <option value="1">Option 1</option> 
        <option value="2" ng-repeat="location in userLocationList"> {{ location.city }},&nbsp; {{location.state}}, &nbsp;<b>{{location.country}}</option> 
         </select> 

Controller

$scope.searchlocation = function(search) 
    { 
     /* 
     if (search.length < 3) { 
      $scope.userLocationList = null; 
      return false; 
     }*/ 



     $http.get(
       location.protocol + '//' + location.host 
         + contextPath + '/services/searchLocation', { 
        params : { 
         "search" : search 

        } 
       }).then(function(response) { 

      $scope.userLocationList = response.data; 




      if ($scope.userLocationList.length == 0) { 
       console.log('no location') 
      } 
     }, function(error) { 

      console.log("error while searching"); 
      console.log(error); 
     }); 

    } 

$(document).ready(function() { 
$("#multipleSelectLocation").select2(); 
}); 

, wenn ich einen Eingabetext und rufen searchLocation Controller haben es funktioniert gut. aber ich kann nicht vom Dropdown aufrufen.

Antwort

0
<select id="multipleSelectLocation" data-placeholder="Select an option" ng-model="search" ng-change="searchlocation()" multiple="true" > 

controlloer

$scope.searchlocation = function() { 
    $scope.selected = $scope.search 

} 
+0

Dank für Ihre schnelle Antwort. Aber warum ruft es die Funktion nicht auf? @beshoy, @ aks1357 – 987654321

0

Es gibt eine AngularJS-native Version von Select2 und Selectize, sollten Sie diese wählen:

https://github.com/angular-ui/ui-select

Das einzige, was zu beachten ist, ui-select Anwendungen on-select anstelle von ng-change

Probe Snippet:

<ui-select multiple sortable="true" ng-model="ctrl.person.selected" theme="select2" class="form-control" title="Choose a person"> 
    <ui-select-match placeholder="Select or search a person in the list...">{{$select.selected.name}}</ui-select-match> 
    <ui-select-choices repeat="item in ctrl.people | filter: $select.search"> 
     <div ng-bind-html="item.name | highlight: $select.search"></div> 
     <small ng-bind-html="item.email | highlight: $select.search"></small> 
    </ui-select-choices> 
    </ui-select> 

Controller:

app.controller('DemoCtrl', function ($scope, $http, $timeout, $interval) { 
    var vm = this; 


    vm.people = [ 
    { name: 'Adam',  email: '[email protected]',  age: 12, country: 'United States' }, 
    { name: 'Amalie', email: '[email protected]', age: 12, country: 'Argentina' }, 
    { name: 'Estefanía', email: '[email protected]', age: 21, country: 'Argentina' }, 
    { name: 'Adrian', email: '[email protected]', age: 21, country: 'Ecuador' }, 
    { name: 'Wladimir', email: '[email protected]', age: 30, country: 'Ecuador' }, 
    { name: 'Samantha', email: '[email protected]', age: 30, country: 'United States' }, 
    { name: 'Nicole', email: '[email protected]', age: 43, country: 'Colombia' }, 
    { name: 'Natasha', email: '[email protected]', age: 54, country: 'Ecuador' }, 
    { name: 'Michael', email: '[email protected]', age: 15, country: 'Colombia' }, 
    { name: 'Nicolás', email: '[email protected]', age: 43, country: 'Colombia' } 
    ]; 

}); 
+0

@ 987654321 - weil jQuery das DOM manipuliert und eckig manipuliertes DOM nicht interpretieren kann, dafür müssen Sie neu manipuliertes DOM mit $ compile kompilieren, was in einer Direktive gemacht wird, also oben habe ich vorgeschlagen, angular-native zu verwenden Lösung von select2 – Aks1357

Verwandte Themen