简体   繁体   English

无法使angular-bootstrap-select正常工作?

[英]Can't get angular-bootstrap-select to work?

Because I want a sexy looking <select> in my angular website I'm trying to get Angular-Bootstrap-Select working. 因为我想要在我的有角度的网站中看起来很性感的<select> ,所以我试图使Angular-Bootstrap-Select正常工作。 The demo seems fairly simple, but I just can't get it to work in my own code. 该演示看起来很简单,但是我无法以自己的代码运行它。

I import all dependencies without problems (jQuery, Bootstrap-select, Angular, etc.) and Bootstrap-select works perfectly fine outside of Angular. 我没有问题地导入了所有依赖项(jQuery,Bootstrap-select,Angular等),Bootstrap-select在Angular之外也可以正常工作。

I installed angular-bootstrap-select using bower, import it successfully in the head and inject it in my angular app: 我使用Bower安装了angular-bootstrap-select,成功将其导入头部并将其注入我的angular应用程序中:

var propertyApp = angular.module('propertyApp', [
    'propertyControllers',
    'ui.router',
    'angular-bootstrap-select'
]);

propertyApp.config(['$stateProvider', '$urlRouterProvider',
    function($stateProvider, $urlRouterProvider){
        $stateProvider
            .state('new', {
                url: '/new',
                templateUrl: '/static/angular/property/partials/newProperty.html',
                controller: 'NewPropertyCtrl'
            });

        $urlRouterProvider.otherwise('/new')
    }
]);

My controller is completely empty: 我的控制器是完全空的:

var propertyControllers = angular.module('propertyControllers', []);
propertyControllers.controller('NewPropertyCtrl', [function(){
}]);

and my template looks like this: 我的模板如下所示:

<div class="row">
    <div class="col-sm-12">
        Before selectpicker
        <select class="selectpicker">
            <option>Mustard</option>
            <option>Ketchup</option>
            <option>Relish</option>
        </select>
        After selectpicker
    </div>
</div>

but the only thing I see in the browser is: Before selectpicker After selectpicker . 但是我在浏览器中看到的唯一一件事是: Before selectpicker After selectpicker

I've got a working codepen here , but I just can't figure out what is so different between that example, and my own code. 我在这里有一个工作正常的codepen ,但是我无法弄清楚该示例和我自己的代码之间有什么不同。

Does anybody see or maybe know what I'm doing wrong here? 有人看到或也许知道我在做什么错吗? All tips are welcome! 欢迎所有提示!

[EDIT] [编辑]

I can get it to work perfectly well in a codepen , but in my own code it still won't to work. 我可以使其在Codepen中完美地工作,但是在我自己的代码中仍然无法正常工作。 My own test is here: http://185.53.129.139:5000/property-admin#/new This really drives me crazy, so anybody helping me out with this would really make my day! 我自己的测试在这里: http : //185.53.129.139 : 5000/property-admin#/new这确实让我发疯,所以任何帮助我解决这个问题的人都一定会过得愉快!

Here is a small plunker I've made with bootstrap-select and angular. 这是我用bootstrap-select和angular制作的一个小矮人。 http://plnkr.co/edit/webnjq6kzLDnnkI9ZO6W http://plnkr.co/edit/webnjq6kzLDnnkI9ZO6W

  angular.module('angular-bootstrap-select', [])
  .directive('selectpicker', ['$parse', function ($parse) {
    return {
      restrict: 'A',
      link: function (scope, element, attrs) {
        element.selectpicker($parse(attrs.selectpicker)());
        element.selectpicker('refresh');

        scope.$watch(attrs.ngModel, function (newVal, oldVal) {
          scope.$parent[attrs.ngModel] = newVal;
          scope.$evalAsync(function () {
            if (!attrs.ngOptions || /track by/.test(attrs.ngOptions)) element.val(newVal);
            element.selectpicker('refresh');
          });
        });

    scope.$on('$destroy', function () {
      scope.$evalAsync(function () {
        element.selectpicker('destroy');
      });
    });
  }
};
}]);

As you'll see, bootstrap-select is wrapped in a directive. 正如您将看到的,bootstrap-select被包装在指令中。 I think that's the easies way. 我认为这是简单的方法。

Regards, Eric 问候,埃里克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM