0

This is the first time I am trying out AngularJS, and I found an example through W3. I am looking to use it to create routing for a new Bootstrap project I will be working on for a client.

The problem is that it doesn't seem to load the javascript I'm using. I'm not all that familiar with javascript and DOM, so not sure why this isn't working.

To show an example of what I'm talking about, I removed everything and just tried it with an image and tooltip. The image on the index page works correctly, but when the test page is loaded, the tooltip stops working. I tried to track if there was an error through developer tools, but nothing is showing up.

INDEX:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="assets/css/bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="assets/css/custom.css">
    <link rel="stylesheet" type="text/css" href="assets/css/font-awesome.min.css">
    <script src="assets/js/jquery-3.3.1.min.js"></script>
    <script src="assets/js/bootstrap.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular-route.js"></script>
    <script>
    var app = angular.module("myApp", ["ngRoute"]);
    app.config(function($routeProvider) {
        $routeProvider
        .when("/", {
            templateUrl : "main.php"
        })
        .when("/home", {
            templateUrl : "main.php"
        })
        .when("/test", {
            templateUrl : "test.php"
        })
    });
    </script>
    <script>
      $(document).ready(function() {
          $('[data-toggle="tooltip"]').tooltip();
      });
    </script>
  </head>
  <body ng-app="myApp">
    <p><a href="#!home">Main</a> | <a href="#!test">Test</a></p>
    <p><img src="http://placehold.it/200x200" class="img-responsive" data-toggle="tooltip" data-placement="top" data-html="true" title="<strong>Test</strong>"></p>
    <div ng-view></div>
  </body>
</html>

TEST:

<p><strong>Test</strong></p>
<p><img src="http://placehold.it/200x200" class="img-responsive" data-toggle="tooltip" data-placement="top" data-html="true" title="<strong>Test</strong>"></p>

Tooltip showing on index before the ng-view DIV.

Tooltip showing before the ng-view DIV

Tooltip not showing on test ng-view.

Tooltip not showing in the test ng-view DIV

UPDATE

Experiencing another issue where when I am trying to revisit /home that calls main.php, it only loads a blank screen with the latest posts and nothing else.

Homepage

Homepage loads fine.

About

About page loads fine.

Back Home

Nothing reloads.

No errors are showing either.

7
  • don't use jQuery with AngularJS (at all). Instead consider using ui.bootstrap, which implements tooltips (with templates) using uib-tooltip-template. After adding a script, don't forget to inject the module: angular.module("myApp", ["ngRoute", "ui.bootstrap"]) Commented Apr 30, 2018 at 14:20
  • Hi Aleksey. Thank you very much for your help with this. I got it to work! Commented Apr 30, 2018 at 14:57
  • I now have another issue. Do you know why it would only load partial of a page? I added this logic to the full website I'm using located at: habbfinity.ca/V4$-$4V2 and when I go to About back to Home, it only reloads the latest posts and nothing else. Updated my question. Commented May 1, 2018 at 13:10
  • firstly, don't use .php files, additionally you could have broken your routing (which I cannot check, since it's on your backend), here is an example with working routing - Plunker Commented May 1, 2018 at 13:42
  • I need PHP files in order to get information to show on quite a few of the pages I'll be using, such as timetable data. On the plunker you're showing me, if I understand it correctly, anything within the bottom scripts would be shown, right? So for the about.html one, it would just show "about"? Commented May 1, 2018 at 13:48

1 Answer 1

1
('[data-toggle="tooltip"]').tooltip();

Does following: search all existing elements and attach tooltip to them. This is completely useless from angular point of view, cause elements are created and removed all the time. (Thats why it wont work for tooltip in ng-view, cause this code is executed before ng-view is processed by angular and filled with content)

thats why in angular all Jquery plugins should be used through directives.

But real advice for angular beginner is remove Jquery completely, so you are not able to use jquery and you are forced to do things in angular way. There are alternatives in angular for great variety of things in jquery i.e. for tooltip - mentioned ui.bootstrap or angular-material.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you both for your responses. After removing the document.ready portion and including the bootstrap ui, I got it to function correctly.
I now have another issue. Do you know why it would only load partial of a page? I added this logic to the full website I'm using located at: habbfinity.ca/V4$-$4V2 and when I go to About back to Home, it only reloads the latest posts and nothing else. Updated my question.

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.