Creating Tabs in AngularJs and Bootstrap - Customize current code for better functionality

I came across a helpful example on stackoverflow demonstrating how to create Tabs using AngularJS and Bootstrap. However, I encountered an issue. The original code is based on an outdated Angular library (1.0.4) and when I attempt to switch to the current version (1.4.7), the script fails to function properly.

You can view the original code on Plunker

Here is the original post

Here are the steps I have taken so far to make changes:

var app = angular.module('plunker', ['ngRoute']);
app.config(function($routeProvider, $locationProvider) {
  $routeProvider
      .when('/jobs', {
        templateUrl: 'jobs-partial.html',   
        controller: JobsCtrl
      })
      .when('/invoices', {
        templateUrl: 'invoices-partial.html',   
        controller: InvoicesCtrl
      })
      .when('/payments', {
        templateUrl: 'payments-partial.html',   
        controller: PaymentsCtrl
      })
      .otherwise({
        redirectTo: '/jobs'
      });
});

What could be causing this problem?

Answer №1

Starting from AngularJS version 1.2:

ngRoute has now been separated into its own module

From AngularJS 1.3 onwards:

...$controller will no longer search for controllers on the global window object. Originally, this behavior was meant for demos and small projects. However, relying on global controller functions led to bad coding practices, so it has been disabled by default.

To fix this, you need to [1] declare `ngRoute` as a dependency in your module, [2] include the `angular-route.js` source file, and [3] explicitly register the controllers like this:

http://plnkr.co/edit/KJyFqf2vf74IidY26vxu?p=preview

[1]

var app = angular.module('plunker', ['ngRoute']);

[2]

<script src="http://code.angularjs.org/1.4.7/angular-route.js"></script>

[3]

app.controller('TabsCtrl', TabsCtrl);
app.controller('JobsCtrl', JobsCtrl);
app.controller('InvoicesCtrl', InvoicesCtrl);
app.controller('PaymentsCtrl', PaymentsCtrl);

Answer №2

In previous versions of angular, it was possible to use global functions as controllers.

However, starting from version 1.3, support for this feature was removed and controllers now need to be registered as a module component.

Additionally, ngRoute is no longer included by default and must be injected as a module dependency.

Below is an example using angular 1.4:

Controllers are registered like so:

angular.module('plunker')
.controller('TabsCtrl', TabsCtrl)
.controller('InvoicesCtrl', InvoicesCtrl)
.controller('PaymentsCtrl', PaymentsCtrl)

In routing, the controller is now specified as a string. Also, make sure to include ngRoute as a module dependency:

var app = angular.module('plunker', ['ngRoute']);

app.config(['$routeProvider', '$locationProvider', function($routeProvider, $locationProvider) {
  $routeProvider.
      when('/jobs', {templateUrl: 'jobs-partial.html',   controller: 'JobsCtrl' }).

View Demo

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method for inserting a URL link using jQuery within a CSS element?

Check out this code snippet: <span class='maincaptionsmall'> Test </span> Due to certain ajax and jquery scripts on my page, I am unable to use HREF as a link. In other words, the following code won't work: <span class=&ap ...

ensure that the radio button is selected on the contact form 7

I'm looking to customize the appearance of my radio buttons within a WP plugin ContactForm7. I found some CSS code on this website: Source CSS code My goal is to change the color of the radio button to green when it's clicked or checked: .wpcf7 ...

Learn how to pass data as props to child components in Vue3

I'm facing an issue where props are initialized, but they disappear after using .mount. Can anyone advise on the correct way to set up props with dynamically loaded components? import {createApp} from 'vue' blockView = createApp(Block); blo ...

The ng-grid is failing to update with the most recent data following the asynchronous XMLHttpRequest response

I am facing an issue with my AngularJS web application. I am trying to upload a file to a server and update ng-grid with the last uploaded file's entry once the upload is complete. Below is the HTML for my grid: <div class="gridholder" data-ng-gri ...

Display the content of an md-dialog with a scroll bar

I am experiencing a problem with printing a long report created using md-list displayed in a md-dialog. When I attempt to print it, only the section that is currently visible on the screen gets printed instead of the entire length of the md-list. I have at ...

Switch the visibility of a div tag using Next.js

Script export default function Navigation(){ let displayMenu = false; function toggleMenu() { displayMenu = !displayMenu; } return ( <> <button onClick={toggleMenu}>Toggle Navigation</button> {/*This code sh ...

Having trouble fetching results from AJAX objects using vanilla JavaScript?

I am currently working on developing my own AJAX prototype without relying on jQuery or any other libraries. However, I am facing an issue where I cannot retrieve any results and the breakpoint set on a specific line is not triggering: The problem seems t ...

Acquiring an Array from AngularJS with Laravel Controller

Just starting out in Laravel and Angularjs, I'm attempting to pass an array from Angularjs to a Laravel controller. While I'm not encountering any errors, I am struggling to retrieve the data in the Laravel controller and use it effectively. Here ...

Top choice for removing items from a list array

Hey there, I'm working with an array of a custom type in Angular: List { task: string; id?: number; status?: boolean; } I'm trying to figure out how to delete elements where List.status == true. I've tried two methods for this. ...

The webpage is not refreshing or reloading despite using window.location.href

When creating a refreshcart() function, the window.location.href is assigned to currentUrl. However, when making an ajax call in the else block with window.location.href = currentUrl, true; it does not successfully refresh the page. $(document).ready(fu ...

What is the speed of retrieving new data once it has been inserted into a firebase real-time database?

In the midst of developing my personal project using next.js, I've encountered an issue with a component that includes a getstaticprops function. This function scrapes a website and then posts the extracted data to a firebase realtime database. Howeve ...

Generate a series of printed pages corresponding to various scroll positions using JavaScript

I have created an HTML page through a program that generates evidence. Exporting this evidence to a PDF format would be beneficial in many situations. This HTML document is divided into two columns, and I need to print multiple pages to PDF where each pag ...

What's the best way to expand the right side of .li following a left offset of -20px

My nested list structure looks like this... https://i.sstatic.net/sggVx.png What you see is a recursive ul/li setup... <div class="family d-block"> <span class="pb-2"> <small class="text-muted">Family:</small><br> < ...

Having issues with inline conditional statements in Angular 5

There is a minor issue that I've been struggling to understand... so In my code, I have an inline if statement like this: <button *ngIf="item?.fields?.assetType !== 'tool' || item?.fields?.assetType !== 'questions'">NEXT< ...

Accessing an AngularJS controller method within an iOS UIWebView

I am currently facing an issue with my dashboard.html page and DashboardController.js in relation to UIWebview. I am attempting to call an AngularJS function from DashboardController.js within the UIWebview, but it seems to not be working. Below is the co ...

Learn how to retrieve the TextBox value from a button click within a Nested Gridview

I am trying to extract the value of a textbox inside a Nested Gridview using jQuery in ASP.NET. When a Button within the Nested Gridview is clicked, I want to display the textbox value in an alert box. Here is an example setup: <asp:GridView ID="Grid ...

Text with equal characters behaving differently based on screen dimensions

I've been working on a website using Bootstrap 4 and I've noticed some strange behavior with strings of characters that have the same length. I'm curious to know why this is happening and if there's a solution for it. Here are the scr ...

Integrating jQuery class into code snippets

I have implemented a fixed header on scroll by adding the class 'fixed' and it is working fine. When I scroll down, after 50px, the 'fixed' class is added to my header container. However, when I scroll back up, the 'fixed' cla ...

What is the best way to make grid-column-start and span work seamlessly together?

I'm having some trouble with creating a table using the grid function in CSS. Specifically, I am stuck on how to add dynamic categories at the top without hardcoding styles for each category count. I have tried using the grid-column-start property to ...

Table header sticking does not work when overflow is set to scroll or auto

After trying numerous solutions without success, I am reposting this question. My table has a horizontal scroll and I attempted to make the table header sticky using position:sticky, but it caused the scrolling functionality to stop working. Is there a wa ...