AngularJS Update an element only works if one of its property is changed
My question may be silly as I'm a beginner in JS and AngularJS but I noticed something. I have a list and a function :
$scope.persons = [{
"id": 1,
"name": "Mark",
"alive": true
}, {
"id": 2,
"name": "Tom",
"alive": true
}, {
"id": 3,
"name": "Jack",
"alive": false
}, {
"id": 4,
"name": "William",
"alive": true
}, {
"id": 5,
"name": "Scott",
"alive": true
}, ];
$scope.kill = function(person) {
person = {
"id": person.id,
"name": person.name,
"alive": !person.alive
};
console.log(person);
}
This list is used to create a table.
<table>
<tbody>
<tr ng-repeat="person in persons">
<td>Name : {{person.name}}</td>
<td>Alive? : {{person.alive}}</td>
<td>
<button ng-click="kill(person)">{{person.alive? 'Kill' : 'Reborn'}}</button>
</td>
</tr>
</tbody>
</table>
When I click the button it is supposed to toggle the value of person.alive. With this code it doesn't work. But, If the functions kill() is :
$scope.kill = function (person) {
person.alive = !person.alive;
console.log(person);
}
It works ! Did I make a mistake in the first function or is it just normal ?
Regards,
javascript angularjs
add a comment |
My question may be silly as I'm a beginner in JS and AngularJS but I noticed something. I have a list and a function :
$scope.persons = [{
"id": 1,
"name": "Mark",
"alive": true
}, {
"id": 2,
"name": "Tom",
"alive": true
}, {
"id": 3,
"name": "Jack",
"alive": false
}, {
"id": 4,
"name": "William",
"alive": true
}, {
"id": 5,
"name": "Scott",
"alive": true
}, ];
$scope.kill = function(person) {
person = {
"id": person.id,
"name": person.name,
"alive": !person.alive
};
console.log(person);
}
This list is used to create a table.
<table>
<tbody>
<tr ng-repeat="person in persons">
<td>Name : {{person.name}}</td>
<td>Alive? : {{person.alive}}</td>
<td>
<button ng-click="kill(person)">{{person.alive? 'Kill' : 'Reborn'}}</button>
</td>
</tr>
</tbody>
</table>
When I click the button it is supposed to toggle the value of person.alive. With this code it doesn't work. But, If the functions kill() is :
$scope.kill = function (person) {
person.alive = !person.alive;
console.log(person);
}
It works ! Did I make a mistake in the first function or is it just normal ?
Regards,
javascript angularjs
It is because you've created a new person based upon the person passed to the function rather than changing the original person.
– Randy Casburn
Nov 21 '18 at 23:39
Ok I see so by doing "this.person.alive = !person.alive" it should work.
– Cybermate
Nov 21 '18 at 23:42
add a comment |
My question may be silly as I'm a beginner in JS and AngularJS but I noticed something. I have a list and a function :
$scope.persons = [{
"id": 1,
"name": "Mark",
"alive": true
}, {
"id": 2,
"name": "Tom",
"alive": true
}, {
"id": 3,
"name": "Jack",
"alive": false
}, {
"id": 4,
"name": "William",
"alive": true
}, {
"id": 5,
"name": "Scott",
"alive": true
}, ];
$scope.kill = function(person) {
person = {
"id": person.id,
"name": person.name,
"alive": !person.alive
};
console.log(person);
}
This list is used to create a table.
<table>
<tbody>
<tr ng-repeat="person in persons">
<td>Name : {{person.name}}</td>
<td>Alive? : {{person.alive}}</td>
<td>
<button ng-click="kill(person)">{{person.alive? 'Kill' : 'Reborn'}}</button>
</td>
</tr>
</tbody>
</table>
When I click the button it is supposed to toggle the value of person.alive. With this code it doesn't work. But, If the functions kill() is :
$scope.kill = function (person) {
person.alive = !person.alive;
console.log(person);
}
It works ! Did I make a mistake in the first function or is it just normal ?
Regards,
javascript angularjs
My question may be silly as I'm a beginner in JS and AngularJS but I noticed something. I have a list and a function :
$scope.persons = [{
"id": 1,
"name": "Mark",
"alive": true
}, {
"id": 2,
"name": "Tom",
"alive": true
}, {
"id": 3,
"name": "Jack",
"alive": false
}, {
"id": 4,
"name": "William",
"alive": true
}, {
"id": 5,
"name": "Scott",
"alive": true
}, ];
$scope.kill = function(person) {
person = {
"id": person.id,
"name": person.name,
"alive": !person.alive
};
console.log(person);
}
This list is used to create a table.
<table>
<tbody>
<tr ng-repeat="person in persons">
<td>Name : {{person.name}}</td>
<td>Alive? : {{person.alive}}</td>
<td>
<button ng-click="kill(person)">{{person.alive? 'Kill' : 'Reborn'}}</button>
</td>
</tr>
</tbody>
</table>
When I click the button it is supposed to toggle the value of person.alive. With this code it doesn't work. But, If the functions kill() is :
$scope.kill = function (person) {
person.alive = !person.alive;
console.log(person);
}
It works ! Did I make a mistake in the first function or is it just normal ?
Regards,
javascript angularjs
javascript angularjs
asked Nov 21 '18 at 23:29
CybermateCybermate
52210
52210
It is because you've created a new person based upon the person passed to the function rather than changing the original person.
– Randy Casburn
Nov 21 '18 at 23:39
Ok I see so by doing "this.person.alive = !person.alive" it should work.
– Cybermate
Nov 21 '18 at 23:42
add a comment |
It is because you've created a new person based upon the person passed to the function rather than changing the original person.
– Randy Casburn
Nov 21 '18 at 23:39
Ok I see so by doing "this.person.alive = !person.alive" it should work.
– Cybermate
Nov 21 '18 at 23:42
It is because you've created a new person based upon the person passed to the function rather than changing the original person.
– Randy Casburn
Nov 21 '18 at 23:39
It is because you've created a new person based upon the person passed to the function rather than changing the original person.
– Randy Casburn
Nov 21 '18 at 23:39
Ok I see so by doing "this.person.alive = !person.alive" it should work.
– Cybermate
Nov 21 '18 at 23:42
Ok I see so by doing "this.person.alive = !person.alive" it should work.
– Cybermate
Nov 21 '18 at 23:42
add a comment |
1 Answer
1
active
oldest
votes
The problem with the first function that you wrote is that you create a new person instead of modifying the one that was passed to you. The second function you wrote is correct because it modifies the correct property on the existing person.
add a 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%2f53421888%2fangularjs-update-an-element-only-works-if-one-of-its-property-is-changed%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
The problem with the first function that you wrote is that you create a new person instead of modifying the one that was passed to you. The second function you wrote is correct because it modifies the correct property on the existing person.
add a comment |
The problem with the first function that you wrote is that you create a new person instead of modifying the one that was passed to you. The second function you wrote is correct because it modifies the correct property on the existing person.
add a comment |
The problem with the first function that you wrote is that you create a new person instead of modifying the one that was passed to you. The second function you wrote is correct because it modifies the correct property on the existing person.
The problem with the first function that you wrote is that you create a new person instead of modifying the one that was passed to you. The second function you wrote is correct because it modifies the correct property on the existing person.
answered Nov 21 '18 at 23:44
Christopher BradshawChristopher Bradshaw
83011129
83011129
add a comment |
add a 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.
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%2f53421888%2fangularjs-update-an-element-only-works-if-one-of-its-property-is-changed%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
It is because you've created a new person based upon the person passed to the function rather than changing the original person.
– Randy Casburn
Nov 21 '18 at 23:39
Ok I see so by doing "this.person.alive = !person.alive" it should work.
– Cybermate
Nov 21 '18 at 23:42