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.

Morgan Klaif
  • 75
  • 12
  • don't use jQuery with AngularJS ([**at all**](https://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background)). Instead consider using [`ui.bootstrap`](https://angular-ui.github.io/bootstrap/#!#tooltip), 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"])` – Aleksey Solovey Apr 30 '18 at 14:20
  • Hi Aleksey. Thank you very much for your help with this. I got it to work! – Morgan Klaif Apr 30 '18 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. – Morgan Klaif May 01 '18 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](https://plnkr.co/edit/0U3KOKB2O5q9vfkD) – Aleksey Solovey May 01 '18 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"? – Morgan Klaif May 01 '18 at 13:48
  • those scripts are of type `ng-template` - basically they are just separate (html) files, also known as _partials_. PHP can render new content, including tables, but that's exactly what angularjs is used for. So you can completely remove php logic from front-end. If you want some data for the tables, just pull out a JSON file, or some data from database with PHP request. [Don't mix them together](https://stackoverflow.com/questions/15623967/should-i-mix-angularjs-with-a-php-framework). Also there could be some issues with rendering as it's not part of the digest cycle – Aleksey Solovey May 01 '18 at 13:54
  • Got it. Thank you very much for the clarification. I'll work on getting everything fixed. – Morgan Klaif May 01 '18 at 13:56

1 Answers1

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.

Petr Averyanov
  • 9,327
  • 3
  • 20
  • 38
  • Thank you both for your responses. After removing the document.ready portion and including the bootstrap ui, I got it to function correctly. – Morgan Klaif Apr 30 '18 at 14:58
  • 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. – Morgan Klaif May 01 '18 at 13:15