AngularJS Update an element only works if one of its property is changed












0















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,










share|improve this question























  • 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
















0















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,










share|improve this question























  • 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














0












0








0








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,










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












1 Answer
1






active

oldest

votes


















0














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.






share|improve this answer























    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%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









    0














    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.






    share|improve this answer




























      0














      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.






      share|improve this answer


























        0












        0








        0







        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.






        share|improve this answer













        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 23:44









        Christopher BradshawChristopher Bradshaw

        83011129




        83011129
































            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%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





















































            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







            Popular posts from this blog

            android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

            SQL update select statement

            'app-layout' is not a known element: how to share Component with different Modules