toggle contents onclick button in angularjs

softvar

Here is the JSFIDDLE DEMO which i created using JQuery.

$(function(){
    $("#describe-block-toggle-btn").click(function(){
        $("#ptor-it-block").toggle();
        $(this).text(function(i, text){
          return text === "Start" ? "Stop" : "Start";
      })
    });
    $("#it-block-toggle-btn").click(function(){
        $(this).text(function(i, text){
          return text === "Start" ? "Stop" : "Start";
      })
    });
});

I need to convert my code from JQuery to Angular.

So far I'm able to convert a lot but still not able to achieve the toggling effect as you can see in JSFIDDLE DEMO as I mentioned above.

Here is the link to ANGULARJS DEMO using plunker.

Any help would be appreciated and if someone can point to good angularjs tutorials apart from the official angularjs site tuts it would be great. Way ahead to learn angularjs in depth.

UPDATE:

Luckily I found a bunch of links to blog posts, articles, videos, etc for learning AngularJS, all at one place. Visit the link here and the explore the infinity.

shaunhusain

http://plnkr.co/edit/VMfKJ2IKJ9WNRPlwE8Su

HTML

<!DOCTYPE html>
<html ng-app="myApp">

<head>
  <link rel="stylesheet" href="style.css" />
  <script src="http://code.angularjs.org/1.1.4/angular.js"></script>
  <script src="script.js"></script>
</head>

<body>
  <div ng-controller="ptorJasmineAddon">
    <div id="ptor-nav-addon">
      <span>
        {{describeButtonData.block}}
        <button ng-click="changeStatus(describeButtonData)">{{describeButtonData.status}}</button>
      </span>
      <span ng-show="describeButtonData.status == 'Stop'">
        {{itButtonData.block}}
        <button ng-click="changeStatus(itButtonData)">{{itButtonData.status}}</button>
      </span>
    </div>
  </div>
</body>

</html>

JS

// Code goes here

var app = angular.module('myApp', []);
app.controller('ptorJasmineAddon', function($scope) {
  $scope.describeButtonData = {
    'block': 'Describe Block',
    'status': 'Start',
    'btnId': 'describe-block-toggle-btn',
    'id': 'ptor-describe-block'
  }
  $scope.itButtonData = {
    'block': 'It Block',
    'status': 'Start',
    'btnId': 'ptor-it-block',
    'id': 'ptor-it-block'
  };
  $scope.shown=true;
  $scope.changeStatus = function(btn) {
    btn.status = btn.status === "Start" ? "Stop" : "Start";
    console.log(btn);
  };
});

Since you mentioned your a beginner too I'd recommend these vid resources if you like learning about things this way:

https://www.youtube.com/watch?v=ZhfUv0spHCY

lots of small tutorials on parts of angular at http://egghead.io

Also there's a #angularjs IRC on freenode.net where people can be pretty helpful and knowledgeable.

If you need to do some DOM manipulation you'll want to use a directive. When you write a directive you basically give it a name then tell it where you should expect to find it (element/tag E, class C, comment M, or attribute A). Here's the directive definition hint from the AngularJS plugin in SublimeText:

directive('directiveName', [ function(){ //would reference in html like <directive-name> note camel case to snake case conversion
  // Runs during compile
  return {
    // name: '',
    // priority: 1,
    // terminal: true,
    // scope: {}, // {} = isolate, true = child, false/undefined = no change
    // controller: function($scope, $element, $attrs, $transclude) {},
    // require: 'ngModel', // Array = multiple requires, ? = optional, ^ = check parent elements
    // restrict: 'A', // E = Element, A = Attribute, C = Class, M = Comment
    // template: '',
    // templateUrl: '',
    // replace: true,
    // transclude: true,
    // compile: function(tElement, tAttrs, function transclude(function(scope, cloneLinkingFn){ return function linking(scope, elm, attrs){}})),
    link: function($scope, iElm, iAttrs, controller) {

    }
  };
}]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change button text onclick with toggle function

From Dev

Toggle button is not working with AngularJS and Angular ui Bootsrap

From Dev

Binding Toggle Button onClick to div

From Dev

AngularJS toggle divs visibility through button

From Dev

AngularJS Hide / Show Row with toggle-Button not work

From Dev

How to sync checkbox inside toggle button in AngularJS?

From Dev

AngularJS - Adding an onClick event to a Button via a Directive

From Dev

jQuery - Enable Disable (Toggle) Selectbox onClick Button

From Dev

Toggle focus onclick of a button

From Dev

AngularJS, BootstrapUI : Toggle a button

From Dev

Angularjs Div Show, Hide Toggle with adding class to active button

From Dev

onclick how to toggle the limitTo value using AngularJS?

From Dev

AngularJS toggle button filters

From Dev

Best way to implement a toggle button in AngularJS Material

From Dev

Real toggle button between different controllers in AngularJS

From Dev

Toggle Button Action OnClick - JQuery + Rails

From Dev

How to toggle between bootstrap button classes onClick?

From Dev

How to toggle the display of a division with onclick event with a button in react?

From Dev

Toggle button is not on

From Dev

How to change button text onclick with toggle function

From Dev

Toggle button is not working with AngularJS and Angular ui Bootsrap

From Dev

Toggle button onClick method does not work

From Dev

AngularJS Hide / Show Row with toggle-Button not work

From Dev

Change toggle button image onclick

From Dev

How to sync checkbox inside toggle button in AngularJS?

From Dev

Toggle buttons contents when single button clicked

From Dev

Trying to toggle Bootstrap Button text onclick with jquery

From Dev

Button over JS onClick or modal toggle

From Dev

Toggle button onclick in react

Related Related

  1. 1

    How to change button text onclick with toggle function

  2. 2

    Toggle button is not working with AngularJS and Angular ui Bootsrap

  3. 3

    Binding Toggle Button onClick to div

  4. 4

    AngularJS toggle divs visibility through button

  5. 5

    AngularJS Hide / Show Row with toggle-Button not work

  6. 6

    How to sync checkbox inside toggle button in AngularJS?

  7. 7

    AngularJS - Adding an onClick event to a Button via a Directive

  8. 8

    jQuery - Enable Disable (Toggle) Selectbox onClick Button

  9. 9

    Toggle focus onclick of a button

  10. 10

    AngularJS, BootstrapUI : Toggle a button

  11. 11

    Angularjs Div Show, Hide Toggle with adding class to active button

  12. 12

    onclick how to toggle the limitTo value using AngularJS?

  13. 13

    AngularJS toggle button filters

  14. 14

    Best way to implement a toggle button in AngularJS Material

  15. 15

    Real toggle button between different controllers in AngularJS

  16. 16

    Toggle Button Action OnClick - JQuery + Rails

  17. 17

    How to toggle between bootstrap button classes onClick?

  18. 18

    How to toggle the display of a division with onclick event with a button in react?

  19. 19

    Toggle button is not on

  20. 20

    How to change button text onclick with toggle function

  21. 21

    Toggle button is not working with AngularJS and Angular ui Bootsrap

  22. 22

    Toggle button onClick method does not work

  23. 23

    AngularJS Hide / Show Row with toggle-Button not work

  24. 24

    Change toggle button image onclick

  25. 25

    How to sync checkbox inside toggle button in AngularJS?

  26. 26

    Toggle buttons contents when single button clicked

  27. 27

    Trying to toggle Bootstrap Button text onclick with jquery

  28. 28

    Button over JS onClick or modal toggle

  29. 29

    Toggle button onclick in react

HotTag

Archive