Manual bootstrapping in AngularJS 1 is ineffective

I am a beginner with angularjs. I attempted to create 2 separate modules in a single js file and then manually bootstrap one of them to an element. This method was suggested in a stackoverflow post. However, it seems to not be working. Each of the modules work perfectly fine on their own. I am uncertain about what the issue might be. Here is the code snippet I am working with:

myApp.js

var app = angular.module("myApp", []);
var myController = app.controller("ctrl1", function($scope){
"use strict";
$scope.fname = "Bill";
$scope.lname = "Goldberg";
$scope.updatevalue = function () {
    $scope.fullname = $scope.fname + $scope.lname;
    //$scope.fname = "newName"
};
});
app.directive("w3TestDirective", function() {
    "use strict";
    return {
        template : "<h1>This is my life!</h1>"
    };
});

var loginController = angular.module('loginController', []);
loginController.controller('loginCntrl', function ($scope) {
"use strict";
$scope.username="email";
$scope.password="password";
$scope.present = false;
$scope.login = function () {
    "use strict";
    if ($scope.username == "Amar" & $scope.password == "Amar") {
        $scope.present=true;
        $scope.loginMessage = "logged in successfully";
    } else {
        $scope.present=true;
        $scope.loginMessage = "Invalid username or password";
    }
}
});

angular.bootstrap(document.getElementById("loginDiv"),['loginController']);

Index.html

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<!--script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-route.js"></script-->
<script src="scripts/services/myApp.js"></script>
<!--script src="scripts/services/login.js"></script-->
<body>

  <div  id="demoDiv" ng-app="myApp" ng-controller="ctrl1">
    <span>Values:</span>
    <input type="text" ng-model="fname"/>
    <input type="text" ng-model="lname"/>
    <button ng-click="updatevalue()">fullname</button>
    <br></br>
    {{ fullname }}
    <br></br>
    <w3-test-directive></w3-test-directive>
  </div>
  <br></br>
  <div id="loginDiv" ng-app="loginController" ng-controller="loginCntrl">
    <input type="text" ng-model="username"/>
    <input type="text" ng-model="password"/>
    <button ng-click="login()">submit</button>
    <br></br>
    <div ng-if="present"> {{ loginMessage }} </div>
  </div>

</body>
</html>

Answer ā„–1

It's important to have a main parent module for your app, even if you can have controllers in multiple modules. In this case, let's use myApp as the parent module and make some adjustments:

Start by placing the ng-app directive on the body element:

<body ng-app="myApp">

  <div  id="demoDiv" ng-controller="ctrl1">
...

Then, remove the ng-app directive from the loginDiv:

<div id="loginDiv" ng-controller="loginCntrl">

Lastly, add the loginController module to the myApp module:

var app = angular.module("myApp", ['loginController']);

With these changes, both controllers can be used within the app seamlessly.

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

Answer ā„–2

The solution to my problem was resolved with the following code snippet:

var app = angular.module("myApp", ['utilModule','loginModule']);
var myController = app.controller("ctrl1", function($scope){
"use strict";
$scope.fname = "Bill";
$scope.lname = "Goldberg";
$scope.updatevalue = function () {
    $scope.fullname = $scope.fname + $scope.lname;
    //$scope.fname = "newName"
};
});
app.directive("w3TestDirective", function() {
    "use strict";
    return {
        template : "<h1>This is my life!</h1>"
    };
});


<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<!--script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular-route.js"></script-->
<script src="scripts/services/myApp.js"></script>
<script src="scripts/services/util.js"></script>
<script src="scripts/services/login.js"></script>
<body ng-app="myApp">

  <div  id="demoDiv" ng-controller="ctrl1">
    <span>Values:</span>
    <input type="text" ng-model="fname"/>
    <input type="text" ng-model="lname"/>
    <button ng-click="updatevalue()">fullname</button>
    <br></br>
    {{ fullname }}
    <br></br>
    <div ng-controller="utilCntrl">
      {{ test }}
    <testDirective></testDirective>
    </div>
    <w3-test-directive></w3-test-directive>
  </div>
  <br></br>
  <div id="loginDiv" ng-controller="loginCntrl">
    <input type="text" ng-model="username"/>
    <input type="text" ng-model="password"/>
    <button ng-click="login()">submit</button>
    <br></br>
    <div ng-if="present"> {{ loginMessage }} </div>
  </div>

</body>
</html>

One minor issue remains, the testDirective tag does not display any content in the browser. However, this is not a critical requirement at the moment, so I will disregard it for now. I want to express my gratitude to @Claies for sharing this solution. Thank you for your time and assistance.

Answer ā„–3

It is important to note that you cannot simultaneously use the ng-app directive and the angular.bootstrap function to initialize your Angular application. You must choose either one or the other.

When working with multiple modules, such as "myApp" and "loginController," it is essential to declare "loginController" as a dependency of "myApp." This ensures that "myApp" serves as the main Angular application module, allowing for the addition of more dependencies, whether they are external or custom.

angular.module('myApp', ['loginController', 'ui-router', 'etc'...])

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

Obtain output from a callback function

Is there a way to set a variable using the code var myVar = myFunction(); when myFunction contains a callback function that prevents it from returning a value directly? I am unsure of how to modify this code in order to allow for a return value from the ...

Conceal YouTube upon opening any model or light box

I have a YouTube video in the center of my page, and when I click on any link from the side navigation, a lightbox appears. However, in IE, the lightbox is behind the YouTube video. I have tried setting the Z-index, but it doesn't seem to work. Is the ...

Steps to transfer the angularfire log in object to a service file

I am looking to enhance the usage of the logged-in object across my site. I am interested in moving this object to a service so that I can efficiently check for user authentication. Below is my login controller: .controller('LoginCtrl', function ...

What is the best way to completely eliminate a many-to-many relationship with a custom property?

I have encountered a situation where I am utilizing an entity setup similar to the one explained in this resource. The problem arises when I try to remove entries from post.postToCategories. Instead of deleting the entire row, TypeORM sets one side of the ...

Issue with Angular JS: ng-repeat not refreshing within ng-view

Whenever I make changes to the data stored in the variable $scope.userMovies, ng-repeat fails to update when it is inside an ng-view. Strangely enough, if it's placed outside of ng-view, everything updates as intended. What could be the reason behind ...

Enable/Disable Text Editing Based on Vue Js Input

Iā€™m working on a way to make certain parts of a string in an input editable or non-editable (readonly) depending on the context in Vue.js. For instance: I have this text: My Name is $John Doe$ Now, I want my Vue.js code to scan the string and allow edi ...

Exploring the power of AngularJS through nested directives

Index.html: <nav-wrapper title="Email Test"> <nav-elem value="first"></nav-elem> <nav-elem value="second"></nav-elem> </nav-wrapper> app.js: app.directive('navWrapper', function() { return { ...

Making HTTP requests with axios in Node.js based on multiple conditions

I'm facing an issue with making get calls using axios to both activeURl and inactiveURl. The goal is to handle error messages from the activeUrl call by checking data from the inactiveUrl. However, I keep receiving error messages for the inactiveURL e ...

Leveraging the power of Javascript and Firebase within the Angular framework

When attempting to integrate Firebase with Angular, I encountered an issue where my localhost was not able to function properly with my JavaScript code. The strange thing is that everything works fine when the code is placed directly in the index.html file ...

Use Object.assign to swap out the current state with a new

Why does the React component with state { key: bool } not omit the existing state key from the new state when a different option is clicked? Link to the code var SampleComponent = React.createClass({ getInitialState: function() { return {}; }, ...

The JQuery datepicker fails to provide the date in the format mm/dd/yy

Recently, I attempted to transform a date into the dd/mm/yy format using JQuery datepicker. Unfortunately, my results displayed in the dd/mm/yyyy format instead. Here is the code snippet that I utilized: chkIn = $.datepicker.formatDate("dd/mm/yy", cinDate ...

Ways to extract information from an Object and save it into an array

In my Angular2 project, I am working on retrieving JSON data to get all the rooms and store them in an array. Below is the code for the RoomlistService that helps me fetch the correct JSON file: @Injectable() export class RoomlistService { constructor( ...

Prevent caching in browser for JavaScript GET requests

My service includes a feature that adds Cache header to responses in order to allow browser caching. However, there are certain JavaScript requests where I need to ensure the data is force reloaded, bypassing the browser cache. Is there a way to accompli ...

Attempting to bundle js files using Browserify, but encountering issues with require('jquery-datetimepicker') not functioning correctly

For bundling my javascript file, I rely on gulp + browserify. I start by running npm install select2 --save require("select2"); module.exports = { init: function() { $('#datetime-start').datetimepicker({ dayOfWeekStart : ...

Is it possible to save jQuery plugin initialization settings for future use?

Is it possible to save a default set of settings for a lightbox jQuery plugin, including options and callback functions, in a variable or array that can be referenced later in different contexts with varying configurations? For example, could I initially ...

Synchronize items across switchable grid and list layouts in Ionic framework

I recently created a switchable layout (grid or list) in Ionic. http://codepen.io/WilsonFpz/pen/jPYqBq <a class="" ng-class="{active: layout == 'list'}" ng-click="layout = 'list'">List</a> <a class="" ng-class="{a ...

What is the importance of including parentheses when passing a function to a directive?

Hello, I'm currently a beginner in Angular and I am experimenting with directives. Here is the code snippet that I am using: HTML <div ng-app="scopetest" ng-controller="controller"> <div phone action="callhome()"> </div> </div ...

Ways to recycle a section of Javascript for multiple uses on a single page

As a newcomer to website building, I have a query regarding the usage of a JavaScript function designed for a slideshow. My one-page website comprises multiple slideshow units with their own divs, holders, and counters (e.g., 1/4). A JavaScript code contro ...

Eliminate JSON data that pertains to dates that are either in the past or future

I am working on integrating upcoming classes and past classes components into my application. I have successfully stored the schedule of classes and can retrieve them using backend services. However, I need to display only the upcoming classes in one compo ...

What steps should I take to resolve the issue where Angular project error states that the data path "/polyfills" must be a string?

I am struggling with deploying my Angular app to Firebase and/or running it successfully locally using NodeJS version 18. To access the package.json and source code, you can click on this link: https://github.com/anshumankmr/jovian-genai-hackathon/blob/mas ...