After changing pages on small site using ngroute, my jquery toggle doesn't work

Issue

Full code:
Plunkr Link

I have been working on a basic AngularJS website and I recently ran into an issue which prevents me from using a jquery toggle after I navigate to a different page. I believe my issue is with the routing since the toggle works if I keep it on my index.html file as well as when I separate page one and put it into codepen. I am unable to find anything too similar around the web that could help me, which is possibly because I have been asking the question incorrectly, however at this point I think I need to ask for help. This may be just a fundamental lack of understanding of ng-route or angular controllers on my end so please bear with me if that’s the case/

$(document).ready(function() {
  $("#SmMagCld").click(function() {
    $("#SMC").toggle(350);
  });
});
<!doctype html>
<html>

<head>

  <link rel="stylesheet" href="style.css">

  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular-route.js"></script>
  <script src="script.js"></script>

</head>

<body ng-app="myApp">


  <p>PLS JUST LET ME ROUTE TO MY OTHER PAGES AND STILL MAINTAIN FUNCTIONALITY MR ANGULAR... :(</p>
  <br>
  <a href="#/!" style="font-size: 10px">:(</a>
  <a href="#!page1" style="font-size: 25px">Page1</a>
  <a href="#!page2" style="font-size: 25px">Page2</a>

  <div ng-view></div>
  <script>
    var app = angular.module("myApp", ["ngRoute"]);
    app.config(function($routeProvider) {
      $routeProvider
        .when("/", {
          templateUrl: "main.html"
        })
        .when("/page1", {
          templateUrl: "page1.html"
        })
        .when("/page2", {
          templateUrl: "page2.html"
        });
    });
    app.controller('mainCtrl', function($scope) {
      $scope.message = 'welcome hooooooooome';
    });
    app.controller('page1Ctrl', function($scope) {
      $scope.message = 'who is on first';
    });
    app.controller('page1Ctrl', function($scope) {
      $scope.message = 'what is on second';
    });
  </script>
</body>

</html>

I would also like to say that no errors appear on my localhost when I put it up nor on the plunkr when I run it.
It isn’t only the toggle that stops working after routing to a different page but I believe if I can figure out why that isn’t working I can apply it to the other similar issues. I created the first site I linked to try and eliminate other possible variables that are causing the issue but if you want to see some other instances of my routes not working, here is another hhttps://plnkr.co/edit/98uEPuLJl8cKGjbrTGsn?p=preview that probably is littered with other mistakes. (not enough reputation to post more than two links so I added an extra h in front of the URL)

I’m fairly new to JavaScript/html, not to mention angularJS, as such any advice about cleaning or improving the code would be much appreciated. Please let me know if any additional information is needed or if I didn’t explain my problem clearly.

Solution

  1. If it’s an angular app your views should be partial (It should not have html or body tags)
  2. Load jquery before angular.
  3. For events you can have ng-click, ng-mouseover, ng-keypress, etc Angularjs html events. And you can write functions on them which has dom manipulation using jquery, inside that page’s controller.
  4. onclick doesn’t work on images with jquery in document.ready because this event checks only dom tree ready not complete multimedia load.
    You can have the onclick binding either by angularjs directives or by binding it inside controller (where it executes on complete load of template).
    Here’s fixed plunker link : https://plnkr.co/edit/nZJtK1z4FkSIdiQS8Rk8?p=preview

Answered By – Shantanu

Answer Checked By – Mildred Charles (AngularFixing Admin)

Leave a Reply

Your email address will not be published.