cpu*_*007 5 dropdownbox angularjs bootstrap-select
我有这个问题,希望得到一些如何解决它的建议.
我已将我的html页面的一部分移动到局部视图并通过ng-view加载它现在的问题是下拉选择不再起作用了,我设法恢复了样式但是当我点击它时,它不开放.当它不在局部视图中时,我甚至不需要进行现在似乎需要的javascript调用.
这是我的代码
index.html
<link href="resources/css/bootstrap.min.css" rel="stylesheet">
<link href="resources/css/bootstrap-select.css" rel="stylesheet">
<!-- Bootstrap Core CSS -->
<script src="resources/js/jquery-2.1.1.min.js"></script>
<script src="resources/js/jquery-1.11.0.js"></script>
<script src="resources/js/bootstrap-select.js"></script>
<script src="resources/library/angular.js"></script>
<script src="resources/library/angular-route.js"></script>
<script src="resources/js/MainController.js"></script>
<script src="resources/js/main-app.js"></script>
partialview.html
-------------------
<select class="selectpicker" multiple ng-model="selE">
<option ng-repeat="e in ee">{{e}}</option>
</select>
<script>
$(document).ready(function () {
$('select').selectpicker({
style: 'btn-default',
size: false
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
先感谢您.
混合Angular + jQuery不是一个好习惯.如果你想使用任何jQuery插件,你可以将它包装成自定义角度指令(官方文档).
请参阅此中的selectpicker指令的工作示例 jsFiddle.
HTML:
<select class="selectpicker"
multiple
title='Choose one of the following...'>
<option>Mustard</option>
<option>Ketchup</option>
<option>Relish</option>
</select>
Run Code Online (Sandbox Code Playgroud)
js:
angular.module('demo')
.directive('selectpicker', function () {
return {
restrict: 'C',
link: function (scope, element) {
$(element).selectpicker({
style: 'btn-default',
size: false
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
require: 'ngModel'在您的指令中使用将为您ngModelController提供链接功能的第4个参数(请参阅此处的doc).
因此,您可以轻松地将控制器值与指令范围绑定.
看到other jsFiddle这里.
指令:
angular.module('demo')
.directive('selectpicker', function () {
return {
restrict: 'C',
require: 'ngModel',
link: function (scope, element, attrs, ngModel) {
var $el = $(element);
$el.selectpicker({
style: 'btn-default',
size: false
});
$el.on('change', function (ee, aa) {
ngModel.$setViewValue($el.val());
scope.$apply();
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
控制器:
angular.module('demo')
.controller('MainCtrl', function ($scope) {
$scope.selected = [];
});
Run Code Online (Sandbox Code Playgroud)
HTML:
You have selected : {{ selected | json }}
<select ng-model="selected"
class="selectpicker"
multiple
title='Choose one of the following...'>
<option>Mustard</option>
<option>Ketchup</option>
<option>Relish</option>
</select>
Run Code Online (Sandbox Code Playgroud)
要动态传递选项,只需更新您的指令:
angular.module('demo')
.directive('selectpicker', function ($timeout) {
return {
restrict: 'C',
require: 'ngModel',
replace: true,
template: '<select multiple><option ng-repeat="opt in options">{{ opt }}</option></select>',
scope: {
options: '='
},
link: function (scope, element, attrs, ngModel) {
var $el = $(element);
$timeout(function () {
$el.selectpicker({
style: 'btn-default',
size: false
});
});
$el.on('change', function (ee, aa) {
ngModel.$setViewValue($el.val());
scope.$apply();
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
然后在你的HTML中:
<div ng-model="selected"
class="selectpicker"
options="issues"
title='Choose one of the following...'>
</div>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8522 次 |
| 最近记录: |