What could be causing the ng-model=$scope.model to be undefined when placed within an ng-repeat?

I need some assistance with this issue. When I use $scope.nameStaff in ng-repeat, the value appears as undefined. I'm puzzled by why it's returning undefined.

Here is the code snippet:

<div ng-controller="MyCtrl">
<div ng-repeat="form in form">
<input type="text" ng-model="nameStaff" />
<input type="text" ng-model="idStaff" />
<button ng-click=addDetail()>add
</button>1
</div>
{{form}}
</div>



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

function MyCtrl($scope) {
  $scope.form = [{
    companyName: "company1",
    companyAddress: "company address",
    staff: [{
      name: "men",
      id: "123"
    }]
  }, {
    companyName: "company1",
    companyAddress: "company address",
    staff: [{
      name: "men",
      id: "123"
    }]
  }]
  $scope.addDetail = function() {
    alert($scope.nameStaff);
    $scope.form[0].staff.push({
      name: $scope.nameStaff,
      id: $scope.idStaff
    });
  }
}

Answer №1

Here is the requested code snippet (although I can't imagine why you would need this):

<!-- avoid using children with identical names as their parents -->
<div ng-repeat="f in form">
    Staff Name:
    <input type="text" ng-model="f.staff.name" /> <br />
    Staff Id:
    <input type="text" ng-model="f.staff.id" />
    <button type="button" ng-click="addStaff(f.staff)" />
</div>

Javascript:

$scope.addStaff = function(staff) {
    var staffName = staff.name;
    var staffId = staff.id;
}

Answer №2

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

function MyCtrl($scope) {
    $scope.form = [{
        companyName: "company1",
        companyAddress: "company address",
        staff: [{
            name: "men",
            id: "123"
        }]
    }, {
        companyName: "company1",
        companyAddress: "company address",
        staff: [{
            name: "men",
            id: "123"
        }]
    }]
    $scope.addDetail = function() {
        alert($scope.nameStaff);
        $scope.form[0].staff.push({
            name: $scope.nameStaff,
            id: $scope.idStaff
        });
    }
}

myApp.controller('MyCtrl', MyCtrl);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="MyCtrl">
    <div ng-repeat="f in form">
        <input type="text" ng-model="$parent.nameStaff" />
        <input type="text" ng-model="$parent.idStaff" />
        <button ng-click=addDetail()>add
        </button>
    </div>
    {{form}}
</div>

Feel free to utilize '$parent.nameStaff'.

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 way to position two flexbox children in the center of a parent div with relative positioning?

I need help creating an animated hamburger menu icon with three lines that form into an X shape when clicked. Currently, I'm struggling to align the first horizontal line and the third one properly. Does anyone know how to make two flexbox children o ...

The highmap fails to render on the screen

$(document).ready(function () { Highcharts.mapChart('container1', { chart: { map: 'map', spacingBottom: 20 }, title: { text: 'displayed areas' }, legend: { enabled: ...

Interactive messages displayed based on cursor movement with jQuery

Seeking advice here. I need something similar to a tooltip, but not quite. My website has numerous links scattered around. When these links are clicked, they trigger an ajax form to load at the bottom of the page. Sometimes, the form can be located far dow ...

Is there a way for me to determine if something is hidden?

My goal is to have selector B toggle when selector A is clicked or when clicking outside of selector B. This part is working fine. However, I'm struggling with preventing selector B from toggling back unless selector A is specifically clicked - not w ...

What is the best way to retrieve a return string from an external program using XPCOM in Firefox?

Is there a way to execute an external program in XPCOM and retrieve the actual return string from it, instead of just the return code? I have researched nsICommandLine, nsICommandLineHandler, nsICommandLineRunner, and nsIProcess but it seems like none of ...

Display various divs simultaneously based on the quantity of items in the dropdown menu

In my project, there is a dynamic select list that retrieves items from the database. Users have the ability to add or delete items from this list. Below is some code related to this functionality: <div class="form-group col-md-3"> <la ...

An interactive 3D model emerges against a sleek ebony backdrop on my online platform

I stumbled upon a three.js 3D object featuring a unique touch - a 404 text with a floating orb replacing the zero. Upon importing its code, it rendered successfully, albeit against a black background. Despite my efforts to tweak values and apply background ...

React: Applying the active class to mapped elements

I have a component that iterates over an array to generate 10 navigation items. I want to implement an onClick method that will add an active class to the clicked item. Are there any best practices using Hooks or the newer React patterns for achieving this ...

Express is capable of dynamically modifying routes at runtime

I've set up an express server with my index.js looking like this: let some_parameter = some_value; const configuredHandler = new Handler(some_parameter); const server = express(); server .get("*", configuredHandler.handleRequest) .post("*", ...

"The server responded with a 405 error message indicating that the requested method

I have been working on a registration form project using HTML, JS, NodeJS, and SQLite. However, I am encountering an issue with the fetch function when trying to post the inputted information into the database. I keep receiving a POST 405 (Method Not Allo ...

Update the radio group model's value within the ngChange function

My form consists of 3 radio buttons with the same name="myNumber" and a function to handle the ng-change event. <form name="myForm" ng-controller="ExampleController"> <label> <input type="radio" ng-model="number" name="myNumber" value ...

What is the most secure approach for defining the file path of an HTML file within an express.js application?

In my directory setup, I have the index.js file located at: path/to/home-page/src/routes This is also where you can find the __dirname. The html file resides at: path/to/home-page/public/bootstrap/Homepage/page.html To serve the html document by relati ...

Find the index of the final empty element in an array using AngularJS

var caseOne = [ {"name":"Felicity", "gender":"F", ... ... ,"type":"Admin"}, {"name":"Tony", "gender":"M", ... ... ,"type":""}, . . . . {"name":"Super Man", "gender":"M", ... ... ,"type":""}, {"name":"Wonder Women", "ge ...

Facing a problem with the carousel in Angular 6

I am currently working with Angular 6 and I have a topAdvertisementList[] that is supposed to return 2 records to be displayed in my carousel with a fixed image, but for some reason, only one record is showing up in the carousel! I suspect there might be a ...

Working with AngularJS to parse a JSON file

I'm struggling to access the object in a json file. This is my code: .controller("ListController", ["$scope", "$http", function($scope, $http){ $http.get('../scripts/bbtv.json').success(function(data) { $scope.artists ...

The function signature '(newValue: DateRange<dateFns>) => void' does not match the expected type '(date: DateRange<unknown>, keyboardInputValue?: string | undefined) => void' as per TypeScript rules

I'm currently utilizing the MUI date range picker from https://mui.com/x/react-date-pickers/date-range-picker/. Here's my code snippet: <StaticDateRangePickerStyled displayStaticWrapperAs="desktop" value={valu ...

Implementing Angular 4 to fetch information from a JSON file

As a beginner in Angular, my current task involves loading data from a JSON file upon click, which I have successfully achieved so far. However, I am facing an issue where I'm unable to load the first JSON object before clicking, meaning that I want ...

The Google Drive API in Node.js is notifying the deletion of files

I encountered an issue with the Google Drive API in my application. Even after deleting files from Google Drive, the listfiles function still returns those deleted files. Is there a solution to prevent this from happening? Below is the function of my API: ...

Implementing AJAX to dynamically update product categories on the homepage

I am embarking on a project that I find quite challenging and would greatly appreciate any help in the form of ideas, tutorials, or examples. Our client has requested a step-by-step system to be implemented on the homepage, allowing customers to choose be ...

Filter card-columns field using Bootstrap and JQuery search functionality

Can you explain how to apply the filter based on card-title? Check this out: https://codepen.io/anon/pen/zyqemK JQUERY: $(document).ready(function(){ $("#myInput").on("keyup", function() { <NEED HELP HERE> }); }); HTML: <body> ...