Guidance for optimizing workflow in an 8-step process using ANGULAR.js

I am currently developing an application using Angular.js and I find myself a little confused about utilizing Angular to build it.

Below, you will see a preview of what I have so far - it may not look great, but it functions.

I believe there are more effective ways to accomplish this task, and I would appreciate input from other users considering the following:

The application will: 1) gather inputs across 8 steps 2) based on those inputs, display specific results 3) allow users to navigate to any state at any time

// Setting up an application module
var app = angular.module('website', ['ngSanitize','ngAnimate','ui.router']);

app.config(function($stateProvider, $urlRouterProvider) {

    $urlRouterProvider.otherwise("/home");

    $stateProvider

        .state('home', {
            url: "/home",
            templateUrl: "js/partials/home.html",
            controller: HomeCtrl
        })

        // Additional states for step-by-step process...

});

// Function to calculate new percentage value based on step
function getNewPercentageValue(step, percent){
    var NewPercentage = 0;
    if(percent){
        NewPercentage = percent * step;
    }else{
        $rootScope.values.ActualPercentage = (100/8);
        NewPercentage = $rootScope.values.ActualPercentage * step;
    }
    return NewPercentage;
}

// Controller for Home page
function HomeCtrl($scope, $http, $rootScope, $state) { 
    /* DEFAULT VARIABLES */
    $rootScope.values = {
        ActualPercentageSteps: (100/8),
        ActualPercentage: 0
    };
}

// Controllers for each step in the process...

Answer №1

If you want to define your controllers, you can use app.controller("MyCtrl", function($scope){}) and avoid the need for globally defined functions by referencing them with a quoted string like controller:"MyCtrl".

Additionally, consider moving your common data into a service or factory since both act as singletons and will maintain the data throughout the application's lifespan. Here is an example on Plunker:

http://plnkr.co/edit/4OYWi35Ke2GGDB6wY2W9

The key point to remember is to use angular.copy when replacing the entire object instead of just =, as both controllers reference the same object. Therefore, it is important not to create a new object and point the service to that object to prevent disconnections.

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

app.controller('MainCtrl', function($scope, Service) {
  $scope.items = Service.items;

  $scope.someModel = {};

  $scope.addItem = function(item){
    Service.addItem(item);
  }
  $scope.resetItems = function(){
    Service.resetItems();
  }
});

app.controller('AnotherCtrl', function($scope, Service) {
  $scope.items = Service.items;
});

app.service('Service', function($timeout){
  var someService = {
    items:[],
    id:0,
    addItem:function(item){
      someService.items.push({label:item, id:someService.id++});
    },
    resetItems:function(){
      someService.id=0;

      //simulated HTTP call
      $timeout(function(){
        angular.copy([], someService.items);
      })
    }
  };
  return someService;
})

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

Updating a list row in Jquery Mobile with images and text!

Hello all, I recently started exploring Jquery and I need some help. My goal is to load a list dynamically, without the need for refreshing the page. The data will be coming from an array that needs to be parsed and displayed in the list. The list shou ...

Execute function upon initial user interaction (click for desktop users / tap for mobile users) with the Document Object Model (DOM)

Looking to trigger a function only on the initial interaction with the DOM. Are there any vanilla JavaScript options available? I've brainstormed this approach. Is it on track? window.addEventListener("click", function onFirstTouch() { console.l ...

Having trouble invoking the .js file with form POST

I've encountered a problem with my code that is technically "working," but it's not functioning as intended. Within the header of my page, I have the following code snippet: <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jqu ...

Using Regex to Retrieve Text Between Two Specific Characters in Javascript

Is there a way to extract specific text from a string using Javascript? For example, consider the following string: "start-extractThis-234" The numbers at the end can vary, but the hyphens are consistent. I believe using regex to capture the text betwe ...

Is there a shared code base in Angular for multiple ng-switch cases?

Is there a way to replicate the behavior of this switch statement using Angular's ng-switch directive? switch(variable){ case 'sample1': case 'sample2': //Common functionality for both 'sample1' and 'sample2&apos ...

Steps for removing multiple parameters from a URL using JavaScript

I need help creating a function that can remove multiple parameters without reloading the page using replaceState. The current function I have only works for one parameter. How can I modify it to work for all pages with more than one parameter that needs t ...

Why is Greasemonkey attempting to login without the user and password being entered in the form?

I am trying to automate the submission of a form on a webpage using a Greasemonkey script. The username and password fields in the form are already filled by Firefox when the page loads, so all I need is for the form to automatically submit and login. Her ...

Utilize the power of Facebook login in your Parse client side application by integrating it with the user object

Currently, I am in the process of implementing a login system using both the Parse and Facebook Javascript SDK. While I have successfully implemented authentication on the client side, I am now facing the challenge of accessing the user object (generated ...

Utilizing an array to cycle through various images

Initially, I'm facing an issue with morscreens[i] not displaying the desired image from the array. When left as it is, it ends up showing a [<] button followed by "morscreens[i]" and then a [>] button. However, enclosing morscreens[i] in quotes ...

Error encountered during React application compilation due to SCSS loading issue

I've encountered an error while trying to develop a React application with Webpack. The issue seems to be related to the main SCSS file: ERROR in ./resources/scss/style.scss (./node_modules/css-loader/dist/cjs.js??ref--6-2!./node_modules/sass-loader/ ...

A guide on fetching the selected date from a datepicker in framework7 with the help of vuejs

Here is a snippet of the code for a component I am working on: <f7-list-input label=“Fecha de nacimiento” type=“datepicker” placeholder=“Selecciona una fecha” :value=“perfil.fecha_nacimiento” @input=“perfil.fecha_nacimiento = $event.t ...

Using Choices.js to inject live data into the select dropdown options

I am currently utilizing Choices.js for multi-select functionality and incorporating a select with a search box. Even though I am using Angular.js to populate the data, it does not appear to be functioning correctly. Is there anyone who can assist me in dy ...

Update: When a variable changes during onUploadProgress in Vue.js, the DOM is not re

Having a bit of an issue here. I'm working on an app where users can upload images using axios. In order to enhance the user experience, I'm trying to implement a loading bar. Here's what it looks like: <div class="loadingbox" :style="{ ...

class automatically receives an unwanted display: none attribute

After I added a copy link inside my panel and clicked to open it, an automatic display:none was triggered. To fix this issue in my jQuery code, I included the line: showme.style.display = "none";. This allowed me to open the panel successfully, but now I& ...

How can we smoothly animate scrolling to the top of an element using jQuery?

I am attempting to implement a function where elements scroll to the top with animation upon being clicked. However, I am facing challenges in making this work successfully. You can view my code in action here: https://jsfiddle.net/3dqzsu2m/1/ Here is ...

Comparing SHA and Python hashlib outputs show discrepancies with identical inputs

These code snippets both use Nodejs and Python to calculate a hash from the same input content, however they seem to be generating different results which is quite puzzling. // npm install jssha const jssha = require("jssha"); var s = new jssha(& ...

Ways to pass a message from index.html to a Vue.js 3 instance

Picture this scenario: You have a Vue index.html file that also loads a custom script: <!DOCTYPE html> <html lang="en"> <head> ... ... <script type="text/javascript"> languagePluginLoader.then(fun ...

Using Node.js to Insert Data into MySQL

I recently started experimenting with node.js and decided to use node-mysql for managing connections. Even though I am following the basic instructions from the github page, I am unable to establish a simple connection successfully. var mysql = require(& ...

Tips for preventing a page refresh using HTML, JQuery, AJAX, and PHP

I need assistance with transferring the value of a selected radio button to a PHP session variable using Javascript/AJAX. Everything seems to be working fine, except that the page refreshes each time. Is there a way to prevent this from happening? Below i ...

Validating multiple conditions in Typescript by passing them as function parameters

As a beginner in TS/JS, I am looking to validate multiple conditions passed as arguments to a function. For instance, currently I am verifying the user role name, but in the future, I may need to check other conditions. validateUserDetails(): Promise< ...