2

I have two divs. I need to show one at a time.

<div ng-show="viewdiv">
</div>
<div ng-show="adddiv">
</div>

<a style="" href="#" ng-click="viewdiv=true" >View</a>

How to set adddiv = false? by this same ng-click.

ng-click="viewdiv=true,adddiv=false"

Is not working.

4 Answers 4

3

Use only one variable like viewdiv and use it like

    <div ng-show="viewdiv">
    </div>
     <div ng-show="!viewdiv">
     </div>

    <a style="" href="#" ng-click="viewdiv=true" >View</a>
Sign up to request clarification or add additional context in comments.

Comments

0

Using semi-colon to separate statements will work

<a style="" href="#" ng-click="viewdiv=true;adddiv=false" >View</a>

Comments

0

<button ng-click="showDiv = !showDiv">test </button>
<div ng-show="showDiv" >
   Div
</div>

See sample snippet here

3 Comments

first initialize what is showDiv? it is string boolean where it is?
No need to initialize.See this snippet jsfiddle.net/HB7LU/26537
yes works. All users didn't have code readablility stuff. using ng-init is the best way to init the variable first and use it
0

HTML

<div ng-show="viewDiv">
  div1
</div>
<div ng-show="!viewDiv">
  div2
</div>
<a style="" href="#" ng-click="viewDiv = !viewDiv" >Click</a>

Controller

 $scope.viewDiv = true;

See http://jsfiddle.net/y9wy70ng/


Additionally you may play with

<a style="" href="#" ng-click="viewDiv = !viewDiv">Change to 
   <span ng-show="viewDiv">div2</span>
   <span ng-show="!viewDiv">div1</span>
</a>

See http://jsfiddle.net/sdp9t96b/

2 Comments

This should be the way to do that.
Edited (ng-click moved to <a>).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.