Passed parameter to reference model in Angular?
I'm iterating from an array of JSON objects and would like to update the value of a item on change of an input field. I passed the item it should reference to inside the function call but it seems that the model is not being update along with it. The snippets of my code can be seen below:
peopleObject = [
{name: "Alice", address: "Tokyo", isModified: false},
{name: "Bob", address: "Manila", isModified: false}
]
formModified(isModified): void {
isModified = true;
}
<div *ngFor="let person of peopleObject; let i=index">
<input [(ngModel)]="person.name" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.name}}" class="form-control">
<input [(ngModel)]="person.address" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.address}}" class="form-control">
<button (click)="savePersonDetails(person.name)" [disabled]="!person.isModified">Save</button>
</div>
javascript angular
add a comment |
I'm iterating from an array of JSON objects and would like to update the value of a item on change of an input field. I passed the item it should reference to inside the function call but it seems that the model is not being update along with it. The snippets of my code can be seen below:
peopleObject = [
{name: "Alice", address: "Tokyo", isModified: false},
{name: "Bob", address: "Manila", isModified: false}
]
formModified(isModified): void {
isModified = true;
}
<div *ngFor="let person of peopleObject; let i=index">
<input [(ngModel)]="person.name" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.name}}" class="form-control">
<input [(ngModel)]="person.address" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.address}}" class="form-control">
<button (click)="savePersonDetails(person.name)" [disabled]="!person.isModified">Save</button>
</div>
javascript angular
let say you want to change the name of the person, so when you change it from the input field you want to update that name in the peopleObject object right?
– Sachi.Dila
Nov 21 at 1:38
add a comment |
I'm iterating from an array of JSON objects and would like to update the value of a item on change of an input field. I passed the item it should reference to inside the function call but it seems that the model is not being update along with it. The snippets of my code can be seen below:
peopleObject = [
{name: "Alice", address: "Tokyo", isModified: false},
{name: "Bob", address: "Manila", isModified: false}
]
formModified(isModified): void {
isModified = true;
}
<div *ngFor="let person of peopleObject; let i=index">
<input [(ngModel)]="person.name" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.name}}" class="form-control">
<input [(ngModel)]="person.address" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.address}}" class="form-control">
<button (click)="savePersonDetails(person.name)" [disabled]="!person.isModified">Save</button>
</div>
javascript angular
I'm iterating from an array of JSON objects and would like to update the value of a item on change of an input field. I passed the item it should reference to inside the function call but it seems that the model is not being update along with it. The snippets of my code can be seen below:
peopleObject = [
{name: "Alice", address: "Tokyo", isModified: false},
{name: "Bob", address: "Manila", isModified: false}
]
formModified(isModified): void {
isModified = true;
}
<div *ngFor="let person of peopleObject; let i=index">
<input [(ngModel)]="person.name" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.name}}" class="form-control">
<input [(ngModel)]="person.address" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.address}}" class="form-control">
<button (click)="savePersonDetails(person.name)" [disabled]="!person.isModified">Save</button>
</div>
peopleObject = [
{name: "Alice", address: "Tokyo", isModified: false},
{name: "Bob", address: "Manila", isModified: false}
]
formModified(isModified): void {
isModified = true;
}
<div *ngFor="let person of peopleObject; let i=index">
<input [(ngModel)]="person.name" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.name}}" class="form-control">
<input [(ngModel)]="person.address" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.address}}" class="form-control">
<button (click)="savePersonDetails(person.name)" [disabled]="!person.isModified">Save</button>
</div>
peopleObject = [
{name: "Alice", address: "Tokyo", isModified: false},
{name: "Bob", address: "Manila", isModified: false}
]
formModified(isModified): void {
isModified = true;
}
<div *ngFor="let person of peopleObject; let i=index">
<input [(ngModel)]="person.name" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.name}}" class="form-control">
<input [(ngModel)]="person.address" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.address}}" class="form-control">
<button (click)="savePersonDetails(person.name)" [disabled]="!person.isModified">Save</button>
</div>
javascript angular
javascript angular
edited Nov 21 at 1:44
asked Nov 21 at 1:00
caricature
235
235
let say you want to change the name of the person, so when you change it from the input field you want to update that name in the peopleObject object right?
– Sachi.Dila
Nov 21 at 1:38
add a comment |
let say you want to change the name of the person, so when you change it from the input field you want to update that name in the peopleObject object right?
– Sachi.Dila
Nov 21 at 1:38
let say you want to change the name of the person, so when you change it from the input field you want to update that name in the peopleObject object right?
– Sachi.Dila
Nov 21 at 1:38
let say you want to change the name of the person, so when you change it from the input field you want to update that name in the peopleObject object right?
– Sachi.Dila
Nov 21 at 1:38
add a comment |
1 Answer
1
active
oldest
votes
If what you are trying to do is subscribe to its input change. You can implement it this way:
<input [ngModel]="person.name" // Property Binding; Removing the event binding ()
(ngModelChange)="person.isModified = true" // Event Binding; directly change the person.isModified to true w/o calling the function
[placeholder]="person.name" // You can also use property binding here if you have a dynamic value to assign
class="form-control">
Had created a Stackblitz link for you reference.
Default name: Alice, when i added 's' - its isModified is changed to true
it isn't working for me :( isModified is not updating in the model
– caricature
Nov 21 at 1:14
May I know if what you are trying to do is subscribe to whatever input change it occurs, it will call the formModified function ? if that's the case, i had updated my answer
– KShewengger
Nov 21 at 1:24
yes that's the idea :) i tried(input)
but it isn't changing the model as well
– caricature
Nov 21 at 1:29
had updated my answer and provided a stackblitz link for you reference. What you're lacking was to set 'this' on your isModified inside the formModified function. Dont forget to initialize isModified at the top of you component's constructor
– KShewengger
Nov 21 at 1:40
I'm iterating through an array of JSON objects so I can't usethis
:( I have updated my question to include sample data
– caricature
Nov 21 at 1:45
|
show 1 more comment
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403861%2fpassed-parameter-to-reference-model-in-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If what you are trying to do is subscribe to its input change. You can implement it this way:
<input [ngModel]="person.name" // Property Binding; Removing the event binding ()
(ngModelChange)="person.isModified = true" // Event Binding; directly change the person.isModified to true w/o calling the function
[placeholder]="person.name" // You can also use property binding here if you have a dynamic value to assign
class="form-control">
Had created a Stackblitz link for you reference.
Default name: Alice, when i added 's' - its isModified is changed to true
it isn't working for me :( isModified is not updating in the model
– caricature
Nov 21 at 1:14
May I know if what you are trying to do is subscribe to whatever input change it occurs, it will call the formModified function ? if that's the case, i had updated my answer
– KShewengger
Nov 21 at 1:24
yes that's the idea :) i tried(input)
but it isn't changing the model as well
– caricature
Nov 21 at 1:29
had updated my answer and provided a stackblitz link for you reference. What you're lacking was to set 'this' on your isModified inside the formModified function. Dont forget to initialize isModified at the top of you component's constructor
– KShewengger
Nov 21 at 1:40
I'm iterating through an array of JSON objects so I can't usethis
:( I have updated my question to include sample data
– caricature
Nov 21 at 1:45
|
show 1 more comment
If what you are trying to do is subscribe to its input change. You can implement it this way:
<input [ngModel]="person.name" // Property Binding; Removing the event binding ()
(ngModelChange)="person.isModified = true" // Event Binding; directly change the person.isModified to true w/o calling the function
[placeholder]="person.name" // You can also use property binding here if you have a dynamic value to assign
class="form-control">
Had created a Stackblitz link for you reference.
Default name: Alice, when i added 's' - its isModified is changed to true
it isn't working for me :( isModified is not updating in the model
– caricature
Nov 21 at 1:14
May I know if what you are trying to do is subscribe to whatever input change it occurs, it will call the formModified function ? if that's the case, i had updated my answer
– KShewengger
Nov 21 at 1:24
yes that's the idea :) i tried(input)
but it isn't changing the model as well
– caricature
Nov 21 at 1:29
had updated my answer and provided a stackblitz link for you reference. What you're lacking was to set 'this' on your isModified inside the formModified function. Dont forget to initialize isModified at the top of you component's constructor
– KShewengger
Nov 21 at 1:40
I'm iterating through an array of JSON objects so I can't usethis
:( I have updated my question to include sample data
– caricature
Nov 21 at 1:45
|
show 1 more comment
If what you are trying to do is subscribe to its input change. You can implement it this way:
<input [ngModel]="person.name" // Property Binding; Removing the event binding ()
(ngModelChange)="person.isModified = true" // Event Binding; directly change the person.isModified to true w/o calling the function
[placeholder]="person.name" // You can also use property binding here if you have a dynamic value to assign
class="form-control">
Had created a Stackblitz link for you reference.
Default name: Alice, when i added 's' - its isModified is changed to true
If what you are trying to do is subscribe to its input change. You can implement it this way:
<input [ngModel]="person.name" // Property Binding; Removing the event binding ()
(ngModelChange)="person.isModified = true" // Event Binding; directly change the person.isModified to true w/o calling the function
[placeholder]="person.name" // You can also use property binding here if you have a dynamic value to assign
class="form-control">
Had created a Stackblitz link for you reference.
Default name: Alice, when i added 's' - its isModified is changed to true
edited Nov 24 at 13:57
answered Nov 21 at 1:09
KShewengger
1,195412
1,195412
it isn't working for me :( isModified is not updating in the model
– caricature
Nov 21 at 1:14
May I know if what you are trying to do is subscribe to whatever input change it occurs, it will call the formModified function ? if that's the case, i had updated my answer
– KShewengger
Nov 21 at 1:24
yes that's the idea :) i tried(input)
but it isn't changing the model as well
– caricature
Nov 21 at 1:29
had updated my answer and provided a stackblitz link for you reference. What you're lacking was to set 'this' on your isModified inside the formModified function. Dont forget to initialize isModified at the top of you component's constructor
– KShewengger
Nov 21 at 1:40
I'm iterating through an array of JSON objects so I can't usethis
:( I have updated my question to include sample data
– caricature
Nov 21 at 1:45
|
show 1 more comment
it isn't working for me :( isModified is not updating in the model
– caricature
Nov 21 at 1:14
May I know if what you are trying to do is subscribe to whatever input change it occurs, it will call the formModified function ? if that's the case, i had updated my answer
– KShewengger
Nov 21 at 1:24
yes that's the idea :) i tried(input)
but it isn't changing the model as well
– caricature
Nov 21 at 1:29
had updated my answer and provided a stackblitz link for you reference. What you're lacking was to set 'this' on your isModified inside the formModified function. Dont forget to initialize isModified at the top of you component's constructor
– KShewengger
Nov 21 at 1:40
I'm iterating through an array of JSON objects so I can't usethis
:( I have updated my question to include sample data
– caricature
Nov 21 at 1:45
it isn't working for me :( isModified is not updating in the model
– caricature
Nov 21 at 1:14
it isn't working for me :( isModified is not updating in the model
– caricature
Nov 21 at 1:14
May I know if what you are trying to do is subscribe to whatever input change it occurs, it will call the formModified function ? if that's the case, i had updated my answer
– KShewengger
Nov 21 at 1:24
May I know if what you are trying to do is subscribe to whatever input change it occurs, it will call the formModified function ? if that's the case, i had updated my answer
– KShewengger
Nov 21 at 1:24
yes that's the idea :) i tried
(input)
but it isn't changing the model as well– caricature
Nov 21 at 1:29
yes that's the idea :) i tried
(input)
but it isn't changing the model as well– caricature
Nov 21 at 1:29
had updated my answer and provided a stackblitz link for you reference. What you're lacking was to set 'this' on your isModified inside the formModified function. Dont forget to initialize isModified at the top of you component's constructor
– KShewengger
Nov 21 at 1:40
had updated my answer and provided a stackblitz link for you reference. What you're lacking was to set 'this' on your isModified inside the formModified function. Dont forget to initialize isModified at the top of you component's constructor
– KShewengger
Nov 21 at 1:40
I'm iterating through an array of JSON objects so I can't use
this
:( I have updated my question to include sample data– caricature
Nov 21 at 1:45
I'm iterating through an array of JSON objects so I can't use
this
:( I have updated my question to include sample data– caricature
Nov 21 at 1:45
|
show 1 more comment
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403861%2fpassed-parameter-to-reference-model-in-angular%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
let say you want to change the name of the person, so when you change it from the input field you want to update that name in the peopleObject object right?
– Sachi.Dila
Nov 21 at 1:38