How to pass a reference of scope's model to a function

This question already has an answer here:

  • Javascript by reference vs. by value [duplicate] 4 answers

  • Change your someMethod to the following:

    $scope.someMethod = function(model) {
        model.magic = 321;
    }
    
    // Or accepting a new value
    $scope.anotherMethod = function(model, value) {
        model.magic = value;
    }
    

    HTML:

    <input type="text" data-ng-model="model.magic">
    <button type="button" data-ng-click="someMethod(model)">Do Some Magic!</button>
    
    <!-- Or with passing a new value -->
    <button type="button" data-ng-click="anotherMethod(model, '123')">Do Some Magic!</button>
    

    JSFIDDLE

    链接地址: http://www.djcxy.com/p/20992.html

    上一篇: JavaScript中引用/值传递了什么?

    下一篇: 如何将范围模型的引用传递给函数