我使用本指南创建了一个dropdown list
没有的自定义select
(select
并非所有浏览器都支持自定义)。这种方式结合了一些CSS
和jQuery。directive
我为此创建了一个dropdown list
,一切似乎都可以正常工作,Angular
直到我尝试在内部实现它ng-view
,我的渲染template
根本不适用于我的解决方案(directive
)。
我的代码 - plunk - 在我的 plunk 中,您将能够看到dropdown list
index.html 中的dropdown list
工作以及 temp.html 中不工作的(渲染到ngView
)两者都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-view:
<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;
}
}
}
};
});
对于这个问题,或多或少有类似的问题,但它们对我帮助不大。