Integrating JSFiddle code into my AngularJS application: A guide

As I integrate this jsfiddle code into my application on the company.html page, I am encountering a strange issue. Instead of displaying the expected imagehttps://i.sstatic.net/5XsIa.png, it only shows thishttps://i.sstatic.net/ggrot.png. Can anyone shed some light on why this problem is occurring? I suspect it might be related to the module name or something of that nature.

var app = angular.module('angularjs-starter', []);

app.controller('MainCtrl', function($scope) {

$scope.choices = [{id: 'choice1'}, {id: 'choice2'}];

$scope.addNewChoice = function() {
var newItemNo = $scope.choices.length+1;
$scope.choices.push({'id':'choice'+newItemNo});
};

$scope.removeChoice = function() {
var lastItem = $scope.choices.length-1;
$scope.choices.splice(lastItem);
};

});

I keep receiving this error in the browser console:https://i.sstatic.net/OFjWv.png

Answer №1

You have encountered an error due to your usage of an

Immediately Invoked Function Expression
. To resolve this, modify it as shown below:

/* ------------------------------------------------------- 

* Filename:     Adding Form Fields Dynamically
* Website:      http://www.shanidkv.com
* Description:  Shanidkv AngularJS blog
* Author:       Muhammed Shanid <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="acdfc4cdc2c5c8c7cdc2c2d9deeccbc1cdc5c082cfc3c1">[email protected]</a>

---------------------------------------------------------*/

var app = angular.module('angularjs-starter', []);

app.controller('MainCtrl', function($scope) {

  $scope.choices = [{
    id: 'choice1'
  }, {
    id: 'choice2'
  }];

  $scope.addNewChoice = function() {
    var newItemNo = $scope.choices.length + 1;
    $scope.choices.push({
      'id': 'choice' + newItemNo
    });
  };

  $scope.removeChoice = function() {
    var lastItem = $scope.choices.length - 1;
    $scope.choices.splice(lastItem);
  };

})(angularjs-starter);
fieldset {
  background: #FCFCFC;
  padding: 16px;
  border: 1px solid #D5D5D5;
}

.addfields {
  margin: 10px 0;
}

#choicesDisplay {
  padding: 10px;
  background: rgb(227, 250, 227);
  border: 1px solid rgb(171, 239, 171);
  color: rgb(9, 56, 9);
}

.remove {
  background: #C76868;
  color: #FFF;
  font-weight: bold;
  font-size: 21px;
  border: 0;
  cursor: pointer;
  display: inline-block;
  padding: 4px 9px;
  vertical-align: top;
  line-height: 100%;
}

input[type="text"],
select {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
  <fieldset data-ng-repeat="choice in choices">
    <select>
         <option>Mobile</option>
         <option>Office</option>
         <option>Home</option>
      </select>
    <input type="text" ng-model="choice.name" name="" placeholder="Enter mobile number">
    <button class="remove" ng-show="$last" ng-click="removeChoice()">-</button>
  </fieldset>
  <button class="addfields" ng-click="addNewChoice()">Add fields</button>

  <div id="choicesDisplay" style="visibility:hidden;">
  </div>
</div>

If you Need the choices display to come up just let me know. Will add that! Just copy the code and you are good to go. Make sure the CSS part is inside your style tag while Angular inside your Script tag.

Happy Coding :)

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

The functionality of the Bootstrap carousel for moving to the next and previous images is malfunctioning, as it only

The carousel on my website is not functioning properly. It only displays the first image and does not slide to the next pictures as it should. Here is the code snippet for the carousel: <body> </nav> <div id="carousel1" class="carousel slid ...

Making a Post Request with Vue.js 2 and Axios - Form Submission

I'm currently facing an issue while trying to send my form data using Axios in combination with ExpressJS for the backend. This is the approach I've taken: <template> <form class="" method="post" @submit.prevent="postNow"> <inp ...

Transferring information between controllers within AngularJS

I've been attempting to transfer data between controllers by creating a service for that purpose. However, I seem to be encountering an issue when trying to retrieve the data. I'm unsure of where exactly my mistake lies. Here is the code snippet: ...

Angular JS appears to be failing to properly establish values in the Dropdownlist

I have a project requirement to connect a dropdownlist with MVC and angular JS. Here is my attempt: var app1 = angular.module('Assign', []) app1.controller('SAPExecutive_R4GState', function ($scope, $http, $window) { // alert(UMS ...

Securing specific pages in Angular front-end: A guide to implementing authentication

Interested in developing a web application using Node.js that allows users to log in (authentication). The app will have 3 non-secure pages (/home, /contact, /about) and one secure page (/admin). I've been consulting the Mean Machine book from scotch. ...

Problem with $http.post() in Angular where Codeigniter is not able to receive data

I'm facing a peculiar issue with Codeigniter and Angular. My approach was to configure the controller in the following way: index is a simple Angular page with just one app and one controller get retrieves data from the database set saves data sent u ...

What are the steps to recursively transform a JavaScript object?

I am currently working on recursively transforming a JavaScript object, but I have encountered an issue. The problem is: if any object key has exactly two properties - 'label' and 'value', then the value should change to the label only ...

Prevent collapsing when clicking twice

This section is designed to collapse and expand in its default mode. It will close when the user selects "No" and open/close when the user selects "Yes". I am looking to modify it so that it only closes when the user picks "No" and reopens only when the u ...

ADAL tokens persisting after logout

After successfully logging into my AngularJS app using the Angular ADAL library, obtaining and verifying the token, I have a question: When signing out, should the token be automatically revoked? If not, what is the process to revoke the token manually? ...

Implementing re-render logic for a React functional component based on changes in useState()

I've created a basic functional component that retrieves a date from localStorage and displays it. The component includes two buttons to add or remove weeks from the date, expecting the updated date to be shown. However, I'm facing an issue where ...

The initial execution of the getDocs function may encounter some difficulties

Whenever a user connects from localhost:3000/ (which automatically redirects to /profile), I try to fetch all documents from Firebase. However, it does not work on the first attempt. Strangely, upon refreshing the page, it successfully retrieves the docume ...

What is the best way to wrap the content of a div and include ellipsis at the end?

There is a div on my page that shows some content. What I need is for it to display only the first 10 characters followed by '...' if the content exceeds that limit. 10 characters + '...' I have the option to use either jQuery or PHP ...

Create a collection of SVG illustrations using Vivus.js

Is it possible to draw multiple SVGs using Vivus.js without having to call the function for each individual drawing? I've encountered an issue with the second drawing not animating properly. Any suggestions or experience with this? Check out this pen ...

Trigger a route change in AngularJS via ng-click by passing a parameter

I am trying to update the route of an angularJS application built with the Ionic framework, but for some reason, the route is not changing Here is a snippet from my app.js file: angular.module('starter', ['ionic', 'starter.contro ...

Crafting a Javascript Email Event: A Step-by-Step Guide

Currently working on a website design project for a friend who requires a "Contact Us Page". I am utilizing Bootstrap Studio and have successfully created a page where users can enter their name and email. However, I am facing difficulties in configuring ...

How can I replicate the functionality of a div mimicking a select element using javascript upon clicking away from the element?

My task was to design a pseudo-select element that showcases columns for each row within the select. Due to HTML's restriction on allowing the <option> tag to include HTML, I had to find an alternate solution. One issue I encountered was replic ...

Using jQuery to attach events and trigger them

Within my code, I have the following scenarios: $("#searchbar").trigger("onOptionsApplied"); And in another part of the code: $("#searchbar").bind("onOptionsApplied", function () { alert("fdafds"); }); Despite executing the bind() before the trigge ...

Recognizing unauthenticated users within a stateless REST API

As I work on building a stateless RESTful API that will be utilized by both an iOS app and an AngularJS browser app, there's one particular challenge I'm facing. Auth tokens are necessary for all actions involving authenticated users such as addi ...

Using the React JS useState hook to toggle the state of a JSON object containing an array when a checkbox is clicked

When I submit my state as a stringified variable from a form to a POST request via a lamda server, it gets parsed and sent to sendgrid for templating. To loop over specific parts (multiple checkboxes) in JSON format, each with the same key but different va ...

ReactJS - When a child component's onClick event triggers a parent method, the scope of the parent method may not behave as anticipated

In my current setup, I have a parent component and a child component. The parent component contains a method within its class that needs to be triggered when the child component is clicked. This particular method is responsible for updating the store. Howe ...