0

I have been playing about with angular and forms but I am having some trouble with the code below. I thought that it should add forenames to the ones already displayed by the ng-repeat whenever the form is submitted but the submit button appears to do nothing.

HTML:

<!DOCTYPE html>
    <html lang="en" ng-app="file">
    <head>
        <meta charset="UTF-8">
        <link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
        <script types="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
        <script types=text/javascript" src="formLoops.js"></script>
        <title>Title</title>
    </head>
    <body ng-controller="PersonalDetailsController as person">
        <p ng-repeat="info in person.details">{{info.forename}}</p>

    <form name="PersonalDetailsForm" ng-controller="DataEntryController as dataCtrl" ng-submit="addPerson(person)">
        <blockquote>
            <p>{{dataCtrl.person.forename}}</p>
            <p>{{dataCtrl.person.surname}}</p>
        </blockquote>

        <label>Forename:</label>
        <input ng-model="dataCtrl.person.forename"/>
        <label>Surname:</label>
        <input ng-model="dataCtrl.person.surname"/>
        <input type="submit" value="submit"/>

    </form>

    </body>
    </html>

JS:

var app = angular.module("file", []);

app.controller("PersonalDetailsController", function() {
  this.details = personalDetails;
});

app.controller("DataEntryController", function() {
  this.person = {};
  this.addPerson = function(person) {
    person.personalDetails.push(this.person);
  };
});

var personalDetails = [{
    forename: "John",
    surname: "Doe"
  },
  {
    forename: "John",
    surname: "Smith"
  }
];
0

3 Answers 3

1

You need to do two changes to the code

1) In HTML you need to add the ng-submit="dataCtrl.addPerson(person)" since you are using the controller as syntax 2) You need to change your js code as follows person.personalDetails.push(this.person); to person.details.push(this.person);

This is because your repeat is working with the details array so you need to push the new data to the details array itself. You are trying to push the data inside the global array that is why it is not worked

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

Great. That did it! Thanks for this and the explanation; this has been a massive help.
0

since you are using the controllerAs use the controllerAs reference when you are calling the function. like ng-submit="dataCtrl.addPerson(person)"

<form name="PersonalDetailsForm" ng-controller="DataEntryController as dataCtrl" ng-submit="dataCtrl.addPerson(person)">

5 Comments

Thanks, sorry but I have made that change and it does not seem to work...
it should be like this ng-submit="dataCtrl.addPerson(dataCtrl.person)
Still nothing. Sorry.
I think you have to add the personDetails array inside the scope of PersonalDetailsController
Sorry, I am very new to angular and my JS is basic; I tried what I think you meant by this and it stopped displaying the forenames at all. Any chance I could have an example?
0

You will need to find a way of sharing data between controllers. One of the ways is using an angular service.

Check out this plunk! https://plnkr.co/edit/oimKIgCDKknwsmSWbUsT?p=preview

script.js

var app = angular.module("file", []);

app.controller("PersonalDetailsController", function(PersonService) {
this.details = PersonService.personalDetails;
});

app.controller("DataEntryController", function(PersonService) {
this.person = {};

this.addPerson = function(person) {
    PersonService.personalDetails.push(this.person);
};
});

app.service("PersonService", function() {
this.personalDetails = [{
    forename: "John",
    surname: "Doe"
}, {
    forename: "John",
    surname: "Smith"
}];
});

index.html

<!DOCTYPE html>
<html lang="en" ng-app="file">

<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="bootstrap.min.css" />
    <script types="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    <script types="text/javascript " src="script.js"></script>

    <title>Title</title>
</head>

<body ng-controller="PersonalDetailsController as person ">
    <pre>
            {{person | json}}
        </pre>
    <p ng-repeat="info in person.details ">{{info.forename}}</p>

    <form name="PersonalDetailsForm " ng-controller="DataEntryController as dataCtrl " ng-submit="dataCtrl.addPerson(dataCtrl.person) ">
        <pre>
            {{dataCtrl.person | json}}
        </pre>

        <label>Forename:</label>
        <input ng-model="dataCtrl.person.forename " />
        <label>Surname:</label>
        <input ng-model="dataCtrl.person.surname " />
        <input type="submit " value="submit " />

    </form>

</body>

</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.