问题
My goal is to create a navigation-item directive, that will work with Twitter Bootstrap. My current code places the <li>
tags further down a level, so I presume that's the reason bootstrap's CSS is not compatible.
This is my directives.js file
angular.module('wdiary.directives', [])
.directive('navitem', [function(){
return {
restrict: 'E',
transclude: true,
templateUrl: 'partials/directives/navitem.html',
scope: {} ,
link: function(scope, element, attrs) {
scope.redirectRoute = attrs.redirect;
var r = attrs.redirect;
scope.itemName = r.substring(r.indexOf('/') + 1, r.length);
}
}
}]);
The navitem.html directive template:
<li ng-class = "{active: $route.current.activeNavigationItem == '{{itemName}}'}"><a href="{{ redirectRoute }}" ng-transclude> </a></li>
A part of index.html:
<div class="navbar" ng-controller = "NavigationController">
<div class="navbar-inner">
<a class="brand" href="#/">WDiary</a>
<ul class="nav">
<navitem redirect = "#/write"> Write </navitem>
<navitem redirect = "#/list"> List </navitem>
</ul>
</div>
</div>
And the controllers.js file
angular.module('wdiary.controllers', [])
.controller('NavigationController', ['$scope', '$route', function($scope, $route) {
$scope.$route = $route;
}])
I have inspected the result, this is what I can see:
<ul class="nav">
<navitem redirect="#/write" class="ng-isolate-scope ng-scope">
<li ng-class="{active: $route.current.activeNavigationItem == 'write'}">
<a href="#/write" ng-transclude="">
<span class="ng-scope"> Write </span>
</a>
</li>
</navitem>
</ul>
I think the solution would be to tell Angular not to wrap the list in another tag?
回答1:
Add in the replace: true to your return object in your directive. This will replace it like how you want.
angular.module('wdiary.directives', [])
.directive('navitem', [function(){
return {
replace: true,
restrict: 'E',
transclude: true,
templateUrl: 'partials/directives/navitem.html',
scope: {} ,
link: function(scope, element, attrs) {
scope.redirectRoute = attrs.redirect;
var r = attrs.redirect;
scope.itemName = r.substring(r.indexOf('/') + 1, r.length);
}
}
}]);
来源:https://stackoverflow.com/questions/17154299/directives-messed-up-styling-angularjs