如何显示/使用angularjs隐藏一个div [英] How to show/hide a div using angularjs

查看:412
本文介绍了如何显示/使用angularjs隐藏一个div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图隐藏一个div当我点击隐藏button.But我无法显示一个div,如果我点击显示button.I'm能够一次只做一个动作。

I'm trying to hide a div when I click hide button.But I'm unable to show a div if I click on show button.I'm able to do only one action at a time.

如果我设置$ scope.showDiv = FALSE;为默认,我无法显示在div即使使用低于code:

If I set " $scope.showDiv = false;" as default,I'm unable to show the div even though by using the below code:

 $scope.getShow = function() {
            $scope.showDiv = true;
            window.location = "./sample.html";
        }

任何人都可以请帮我对这个问题...

Can anyone please help me out regarding this issue ...

我的HTML code:

My html code:

<div ng-if="showDiv">Hello</div>
<button style="margin-left: 22px; margin-top: 16px; background-color: #73AD21"
ng-click="getShow()">Show</button>
<button style="margin-left: 22px; margin-top: 16px; background-color: #73AD21"
ng-click="getHide()">Hide</button>

我的js code:

My js code:

$scope.showDiv = false;
$scope.getShow = function() {
        $scope.showDiv = true;
        window.location = "./sample.html";
    }
$scope.getHide = function() {
        $scope.showDiv = false;
        window.location = "./sample.html";
    }

我想从我家page.when访问主页我点击主页显示按钮,它应该重定向到主网页,并显示div.when我点击主页上隐藏按钮,它应该重定向到主页和隐藏div.This是我无法现在做的事。

I'm trying to access main page from my home page.when I click show button in the home page,it should redirect to main page and show the div.when I click hide button in the home page,it should redirect to main page and hide the div.This is what I'm unable to do currently.

推荐答案

您拨打后评论人提及

window.location = "./sample.html";

该页面可能重新加载,code从

the page might reload and code starting from

$scope.showDiv = false;

再次调用。所以你在循环时showDiv变量始终是假的。

is called again. So you are in cycle when showDiv variable is always false.

您可能要更改设置的内容如果在开始发言

What you might want to change is set if statement in the beginning

if(window.location == "./home")
    $scope.showDiv = true;
if(window.location == "./main")
    $scope.showDiv = false;

这篇关于如何显示/使用angularjs隐藏一个div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆