gpt4 book ai didi

javascript - 指令搞砸了样式,AngularJS

转载 作者:行者123 更新时间:2023-11-30 10:30:37 24 4
gpt4 key购买 nike

我的目标是创建一个导航项指令,它将与 Twitter Bootstrap 一起使用。我当前的代码放置了 <li>标记进一步向下一个级别,所以我认为这就是 Bootstrap 的 CSS 不兼容的原因。

这是我的 directives.js 文件

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);
}
}
}]);

navitem.html 指令模板:

<li ng-class = "{active: $route.current.activeNavigationItem == '{{itemName}}'}"><a href="{{ redirectRoute }}" ng-transclude> </a></li>

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>

和 controllers.js 文件

angular.module('wdiary.controllers', [])    
.controller('NavigationController', ['$scope', '$route', function($scope, $route) {
$scope.$route = $route;
}])

我检查了结果,这是我能看到的:

<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>

我认为解决方案是告诉 Angular 不要将列表包装在另一个标记中?

最佳答案

将 replace: true 添加到指令中的返回对象。这将按照您的意愿替换它。

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);
}
}
}]);

关于javascript - 指令搞砸了样式,AngularJS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17154299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com