Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs -> ng-click and ng-show to show a div

I am trying just to display a div when user press a button, seems to be easy, but after spent a lot of time I am getting really crazy with this. My code is

My fiddle: http://jsfiddle.net/jmhostalet/4WK7R/1/

<div ng-app>     <div ng-controller="MyCtrl">         <div><button id="mybutton" ng-click="showAlert()">Click me</button></div>         <div>Value: {{myvalue}}</div>         <div><div ng-show="myvalue" class="hideByDefault">Here I am</div></div>     </div> </div> 

and my controller

function MyCtrl($scope) {    $scope.myvalue = false;    $scope.showAlert = function(){     $scope.myvalue = true;     }; } 

Thank you!

like image 473
jmhostalet Avatar asked Apr 12 '14 20:04

jmhostalet


People also ask

Can we use NG model for Div?

NgModel expects the bound element to have a value property, which div s don't have. That's why you get the No value accessor error. I don't know if the input event is supported on all browsers for contenteditable . You could always bind to some keyboard event instead.

What is Ng-show in AngularJS?

AngularJS ng-show Directive The ng-show directive shows the specified HTML element if the expression evaluates to true, otherwise the HTML element is hidden.

Can we use NG-click and Onclick together?

For a single btn, it's ok to use ng-click or onclick in the ng-app . There is no difference between the two functions. For effective team work, you,d better to have an account with each other. In Angular apps, ng-click is recommended.

Can you use Ng if and Ng-show together?

ng-if is better in this regard. Using it in place of ng-show will prevent the heavy content from being rendered in the first place if the expression is false. However, its strength is also its weakness, because if the user hides the chart and then shows it again, the content is rendered from scratch each time.


1 Answers

Just get rid of the display: none; from your CSS. AngularJS is in control of showing/hiding that div.

If you want to hide it by default, just set the value of scope.myvalue to false initially - which you're already doing.

like image 121
Sergey K Avatar answered Oct 05 '22 20:10

Sergey K