Passed parameter to reference model in Angular?












1















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>












share|improve this question

























  • 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 '18 at 1:38
















1















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>












share|improve this question

























  • 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 '18 at 1:38














1












1








1








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>












share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 1:44







caricature

















asked Nov 21 '18 at 1:00









caricaturecaricature

346




346













  • 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 '18 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 '18 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 '18 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 '18 at 1:38












1 Answer
1






active

oldest

votes


















0














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



Demo






share|improve this answer


























  • it isn't working for me :( isModified is not updating in the model

    – caricature
    Nov 21 '18 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 '18 at 1:24











  • yes that's the idea :) i tried (input) but it isn't changing the model as well

    – caricature
    Nov 21 '18 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 '18 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 '18 at 1:45











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
});


}
});














draft saved

draft discarded


















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









0














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



Demo






share|improve this answer


























  • it isn't working for me :( isModified is not updating in the model

    – caricature
    Nov 21 '18 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 '18 at 1:24











  • yes that's the idea :) i tried (input) but it isn't changing the model as well

    – caricature
    Nov 21 '18 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 '18 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 '18 at 1:45
















0














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



Demo






share|improve this answer


























  • it isn't working for me :( isModified is not updating in the model

    – caricature
    Nov 21 '18 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 '18 at 1:24











  • yes that's the idea :) i tried (input) but it isn't changing the model as well

    – caricature
    Nov 21 '18 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 '18 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 '18 at 1:45














0












0








0







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



Demo






share|improve this answer















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



Demo







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 13:57

























answered Nov 21 '18 at 1:09









KShewenggerKShewengger

1,475614




1,475614













  • it isn't working for me :( isModified is not updating in the model

    – caricature
    Nov 21 '18 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 '18 at 1:24











  • yes that's the idea :) i tried (input) but it isn't changing the model as well

    – caricature
    Nov 21 '18 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 '18 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 '18 at 1:45



















  • it isn't working for me :( isModified is not updating in the model

    – caricature
    Nov 21 '18 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 '18 at 1:24











  • yes that's the idea :) i tried (input) but it isn't changing the model as well

    – caricature
    Nov 21 '18 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 '18 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 '18 at 1:45

















it isn't working for me :( isModified is not updating in the model

– caricature
Nov 21 '18 at 1:14





it isn't working for me :( isModified is not updating in the model

– caricature
Nov 21 '18 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 '18 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 '18 at 1:24













yes that's the idea :) i tried (input) but it isn't changing the model as well

– caricature
Nov 21 '18 at 1:29







yes that's the idea :) i tried (input) but it isn't changing the model as well

– caricature
Nov 21 '18 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 '18 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 '18 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 '18 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 '18 at 1:45




















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini