我用this guide來創建自定義dropdown list
而不是select
(所有瀏覽器都不支持定製select
)。 這種方式結合了一些CSS
和jQUery。我爲這個dropdown list
創建了一個directive
,一切似乎都正常工作,直到我試圖在ng-view
內部實現它,我的解析template
根本無法使用我的解決方案(directive
)。爲什麼自定義下拉列表指令不適用於ngView?
My code - plunk - 在我的普拉克你可以看到index.html中工作dropdown list
和未在temp.html(呈現爲ngView
)工作dropdown list
兩個dropdown lists
使用相同的directive
。
我的index.html:
<body ng-controller='VotesCtrl'>
<p>This is working (no ng-view):</p>
<div dropdown id="dd" class="wrapper-dropdown-3" tabindex="1">
<i class="arrow"></i>
<span>{{statuses[0]}}</span>
<ul class="dropdown">
<li ng-repeat="status in statuses"><a href="#">{{status}}</a></li>
</ul>
</div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<div class="content" ng-view></div>
</body>
我temp.html其通過路由器渲染到NG-視圖:
<p>This is from ng-View, not working:</p>
<div dropdown id="dd" class="wrapper-dropdown-3" tabindex="1">
<i class="arrow"></i>
<span>{{statuses[0]}}</span>
<ul class="dropdown">
<li ng-repeat="status in statuses"><a href="#">{{status}}</a></li>
</ul>
</div>
我的代碼(包括directive
):
// Code goes here
var webApp = angular.module('webApp', []);
//router logic
webApp.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/', {
templateUrl: 'temp.html',
controller: 'tempCtrl'
})
.otherwise({redirectTo: '/'});
}]);
//controllers
webApp.controller ('VotesCtrl', function ($scope) {
$scope.statuses = ["Approved","Pending","Trash","Spam"];
});
webApp.controller ('tempCtrl', function ($scope) {
$scope.statuses = ["Approved","Pending","Trash","Spam"];
});
//services
//directive
webApp.directive('dropdown', function() {
return {
restrict: 'A',
link: function() {
$(function() {
var dd = new DropDown($('#dd'));
$(document).click(function() {
// all dropdowns
$('.wrapper-dropdown-3').removeClass('active');
});
});
function DropDown(el) {
this.dd = el;
this.placeholder = this.dd.children('span');
this.opts = this.dd.find('ul.dropdown > li');
this.val = '';
this.index = -1;
this.initEvents();
}
DropDown.prototype = {
initEvents : function() {
var obj = this;
obj.dd.on('click', function(event){
$(this).toggleClass('active');
return false;
});
obj.opts.on('click',function(){
var opt = $(this);
obj.val = opt.text();
obj.index = opt.index();
obj.placeholder.text(obj.val);
});
},
getValue : function() {
return this.val;
},
getIndex : function() {
return this.index;
}
}
}
};
});
對於這個問題,或多或少有類似的問題,但他們對我沒有多大幫助。
你plunk沒有完全工作。選定的選項不會成爲活動的選項 – Canttouchit
我按照您的意見進行管理以創建適用的內容:http://plnkr.co/edit/TBrlW2kU5oi0xDkEZPe3?p=preview – Canttouchit