我正在测试和应用程序,想做一些非常简单的事情。我想隐藏一个 div,仅在将扩展名添加到 url 时才在浏览器中显示它。例如:
<div ng-hide="true" class="cookie-banner">
Warning you about cookies
</div>
我会通过添加--> ?show=cookie-banner 来看到隐藏的div 到 url,因此本地站点如: ../webapp/#/subscribed 不会显示隐藏的 div,但 ../webapp/#/subscribed?show=cookie-banner 会显示。我不确定我在做什么才能使这项工作成功。请帮忙!
最佳答案
您可以使用angular $location service在您的 Controller 或您创建的自定义指令中,您更喜欢根据 $location.$$absUrl 值设置逻辑。
//in your controller
app.controller('SomeController', ['$scope', '$location',
function ($scope, $location) {
$scope.hideBasedOnUrl = $location.$$absUrl.indexOf('show=cookie-banner') !== -1;
}]);
//html
<div ng-hide="hideBasedOnUrl " class="cookie-banner">
Warning you about cookies
</div>
//in a directive
app.directive("hideBasedOnUrl", function ($location) {
return {
restrict: "E",
link: function (scope, element, attrs, ngModelCtrl) {
if ($location.$$absUrl.indexOf('show=cookie-banner') !== -1){
element.style.display = "none";
}
}
}
});
//html
<div hideBasedOnUrl class="cookie-banner">
Warning you about cookies
</div>
使用该指令,您可以执行更高级的操作,例如在想要隐藏 div 时将 html 中的 url 值作为属性传递。
希望这有帮助!
关于javascript - ng hide,如何有条件地显示带有 url 扩展名的 Angular 内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26262899/