angularjs scope variable change onclick for conditional div

Ratan Kumar

i have two divs i want to show them conditionally with onclick event .

my-angular-app.js

$(document).on('click', '#showless', function(el) {
 var appElement = document.querySelector('[ng-app=myapp]');
 var $scope = angular.element(appElement).scope();
  $scope.$apply(function() {
      $scope.value = false;
    });

});

$(document).on('click', '#showmore', function(el) {
  var appElement = document.querySelector('[ng-app=myapp]');
  var $scope = angular.element(appElement).scope();
  $scope.$apply(function() {
       $scope.value = true;
 });

});

and my div of myapp (myapp.html)

    <div ng-show="desc" id="description" class="text-muted" style="padding-top:5px;padding-left:10px;padding-right:10px;color:#2E2E2E;font-size:11px;">{{myapp.value|truncate}}<span><a id="showmore" href="">more</a></span>
    </div>
    <div ng-show="!desc" id="description" class="text-muted" style="padding-top:5px;padding-left:10px;padding-right:10px;color:#2E2E2E;font-size:11px;">{{myapp.value}}<span><a id="showless" href="">less</a></span>
    </div>

(truncate is a filter i wrote which works fine .)

Davin Tryon

I'm not exactly sure what you are trying to do, but Angular provides ng-click, so you should not have to bind to $(document).on('click').

I'd suggest a simpler approach for conditional show:

<div ng-show="desc" id="description" class="text-muted" style="padding-top:5px;padding-left:10px;padding-right:10px;color:#2E2E2E;font-size:11px;">{{myapp.value|truncate}}<span><a id="showmore" ng-click="desc = true" href="#">more</a></span>
    </div>
    <div ng-show="!desc" id="description" class="text-muted" style="padding-top:5px;padding-left:10px;padding-right:10px;color:#2E2E2E;font-size:11px;">{{myapp.value}}<span><a id="showless" ng-click="desc = false" href="#">less</a></span>
    </div>

The above uses ng-click to set the value of desc. Therefore, you don't need any other logic in the controller to toggle the divs.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Revert change on $scope variable in AngularJs

From Dev

scope variable not updating with ng-change - angularjs

From Dev

AngularJS change scope variable on ng-click

From Dev

scope variable not updating with ng-change - angularjs

From Dev

AngularJS change scope variable on ng-click

From Dev

why angularjs directive inherit the scope but doesn't change the scope variable?

From Dev

Update angularJs directive scope variable on change parent scope

From Dev

AngularJS Change Checked Value of Radio Button Using $scope variable

From Dev

AngularJS - change variable value with if else statement when scope changes

From Dev

AngularJs #how to pass scope variable in on change event in directive in input file

From Dev

get echarts on click event to change value of variable attached to $scope in angularjs

From Dev

AngularJS Get Filtered Scope onClick

From Dev

Scope variable not printing AngularJS

From Dev

Variable scope issue with AngularJS

From Dev

Scope variable not binding in AngularJS

From Dev

Dynamic scope variable in angularjs

From Dev

Dynamic onclick variable scope in javascript

From Dev

AngularJs Render div onclick

From Dev

AngularJs Render div onclick

From Dev

onclick div value change

From Dev

Change div content onclick

From Dev

Change div content with onclick

From Dev

onclick change background of div

From Dev

Div Background Change OnClick

From Dev

Change Javascript variable by onclick

From Dev

Change Javascript variable by onclick

From Dev

AngularJS : Select doesn't change selected option on change of bound scope variable

From Dev

AngularJS : Select doesn't change selected option on change of bound scope variable

From Dev

angularJS $scope change not updating view

Related Related

  1. 1

    Revert change on $scope variable in AngularJs

  2. 2

    scope variable not updating with ng-change - angularjs

  3. 3

    AngularJS change scope variable on ng-click

  4. 4

    scope variable not updating with ng-change - angularjs

  5. 5

    AngularJS change scope variable on ng-click

  6. 6

    why angularjs directive inherit the scope but doesn't change the scope variable?

  7. 7

    Update angularJs directive scope variable on change parent scope

  8. 8

    AngularJS Change Checked Value of Radio Button Using $scope variable

  9. 9

    AngularJS - change variable value with if else statement when scope changes

  10. 10

    AngularJs #how to pass scope variable in on change event in directive in input file

  11. 11

    get echarts on click event to change value of variable attached to $scope in angularjs

  12. 12

    AngularJS Get Filtered Scope onClick

  13. 13

    Scope variable not printing AngularJS

  14. 14

    Variable scope issue with AngularJS

  15. 15

    Scope variable not binding in AngularJS

  16. 16

    Dynamic scope variable in angularjs

  17. 17

    Dynamic onclick variable scope in javascript

  18. 18

    AngularJs Render div onclick

  19. 19

    AngularJs Render div onclick

  20. 20

    onclick div value change

  21. 21

    Change div content onclick

  22. 22

    Change div content with onclick

  23. 23

    onclick change background of div

  24. 24

    Div Background Change OnClick

  25. 25

    Change Javascript variable by onclick

  26. 26

    Change Javascript variable by onclick

  27. 27

    AngularJS : Select doesn't change selected option on change of bound scope variable

  28. 28

    AngularJS : Select doesn't change selected option on change of bound scope variable

  29. 29

    angularJS $scope change not updating view

HotTag

Archive