bootstrap-combobox icon indicating copy to clipboard operation
bootstrap-combobox copied to clipboard

incompatible with angular

Open pc-dedelhart opened this issue 12 years ago • 5 comments

I have been trying to use the bootstrap combobox with angular but the mere act of databinding the select seems to do "funny things" to the select.

The b-c renders and you get typeahead help but the dropdown doesn't show your list of available options.

pc-dedelhart avatar Jun 04 '13 21:06 pc-dedelhart

I'm a backbone guy myself, I'd have to take a look and see what databinding does. Feel free to dig more and offer suggestions.

danielfarrell avatar Jun 05 '13 19:06 danielfarrell

I will try to set up a jsFiddle for you.

bingomanatee avatar Jun 05 '13 20:06 bingomanatee

Hi all, I had this issue too. Turns out that you just need to understand how to make angular and jQuery play nicely together.

Here's what I did to get things working:

My HTML: (Note: the initial placeholder option here is key, stops the selection being null at first)

<div class="well" ng-controller="DeviceSelectCtrl">
        <h2>Device ID:</h2>
        <select class="combobox" ng-model="selectedDevice" ng-options="d.label for d in devices" combobox>
            <option style="display:none" value="">Select a Device</option>
        </select>
    </div>

My Angular controller:

app.controller('DeviceSelectCtrl', ['$scope', function($scope) {
    $scope.comboboxSetup = false;
    $scope.devices = [{label: 'D1', value: 'D1'},
                      {label: 'D2', value: 'D2'}];
}]);

My Angular directive 'combobox'

app.directive('combobox', function(){
    return {
        restrict: 'A',
        link: function(scope, element, attrs){
            scope.$watch("selectedDevice", function(){
                if(!scope.comboboxSetup){
                    $(".combobox").combobox();
                    scope.comboboxSetup = true;
                }
            });
        }
    }
});

jamesdbowman avatar Jul 22 '13 17:07 jamesdbowman

There a simple change in the library that would make this play nicer with angular?

thephw avatar May 13 '14 21:05 thephw

@bowmessage Thanks for the code snippets. I'm making an async HTTP request to get the data, and then use ng-repeat to populate the <option>s. Your solution almost worked for me.

To make it work for me:

  • add another flag that signaled when the async request was done
  • make watch expression for scope.$watch() be a function that returned the flag above
  • add setTimeout to delay calling .combobox() until Angular rendered the data returned from the async call into multiple <option> elements
app.controller('DeviceSelectCtrl', ['$scope', '$http', function($scope, $http) {
    $scope.comboboxSetup = false;
    $scope.asyncReqDone = false;
    $http.get('http://example.com')
      .success(function(data) {
        // do stuff
        $scope.asyncReqDone = true;
      });
}]);
app.directive('combobox', function() {
    return {
        restrict: 'A',
        link: function(scope, element, attrs) {
            scope.$watch(
              function() { return scope.asyncReqDone; },
              function() {
                if(!scope.comboboxSetup && scope.asyncReqDone) {
                    window.setTimeout(function() {
                      $(".combobox").combobox();
                      scope.comboboxSetup = true;
                    }, 1000);
                }
            });
        }
    }
});

davidxia avatar Nov 03 '14 03:11 davidxia