Binding inputs to an array of primitives using ngRepeat => uneditable inputs

后端 未结 5 1384
一整个雨季
一整个雨季 2020-12-02 15:46

Here is a demo to my problem.

$scope.myNumbers = [10, 20, 30];

   
5条回答
  •  一个人的身影
    2020-12-02 16:10

    ngRepeat uses a reference to the source array. Since integer (Number in js) is a value type, not a reference type, therefore cannot be passed by reference in javascript. The change will not be propagated.

    Here is a demonstration:

       var x = 10;
       var ox = {value:10};
    
       var y = x;
       var oy = ox;
    
       y = 15
       oy.value = 15;
    

    What would be the values of x and ox?

    >> x = 10;
    >> y = 15;
    >> ox = {value:15};
    >> oy = {value:15};
    

    All javascript objects are passed by reference and all primitives are passed by value ["string", "number", etc].

    Working plunker http://plnkr.co/edit/7uG2IvAdC2sAEHbdHG58

提交回复
热议问题