无法使用select2

时间:2016-08-08 11:25:07

标签: javascript jquery angularjs

我在下拉列表的on-change事件中点击了数据库。但是我必须在用户选择其中任何一个后制作标签。

jsp文件

<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>

控制器

$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();
}); 

如果我有输入文本并调用searchLocation控制器,它可以正常工作。但是我无法从下拉列表中打电话。

2 个答案:

答案 0 :(得分: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

}

答案 1 :(得分:0)

有一个AngularJS原生版本的Select2和Selectize,您应该使用它:

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

唯一需要考虑的事项是func evaluateRocketProjectileContact(between firstBody: SKPhysicsBody, and secondBody: SKPhysicsBody) { var rocketBody: SKPhysicsBody var projectileBody: SKPhysicsBody if (firstBody.categoryBitMask & projectileCategory) > 0 { projectileBody = firstBody rocketBody = secondBody } else { projectileBody = secondBody rocketBody = firstBody } let rocket = rocketBody.node as! RocketNode let projectile = projectileBody.node as! SKSpriteNode rocket.HP -= 1 // move projectile to rocket and remove its physics body projectile.moveToParent(rocket) projectile.physicsBody = nil if rocket.HP == 0 { // do something explosive animation let explode = SKAction.scaleBy(2.0, duration: 1.0) rocket.runAction(explode, completion: { rocket.removeFromParent() }) } } 使用ui-select代替on-select

示例代码段:

ng-change

控制器:

  <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>