AngularJS Directive Two-Way Data Binding Not Working When Observing Boolean

后端 未结 2 1971
孤独总比滥情好
孤独总比滥情好 2020-12-06 16:27

I have a two-way data binding that is not altering the value of the variable sent to the directive.

My directive watches for a trigger and gives focus to the associa

2条回答
  •  Happy的楠姐
    2020-12-06 16:52

    Directive create a scope. when you pass parameter to scope, object passed by reference and boolean (and number, string...) passed by value.

    For example:

    function changeToFalse(bool) {
        bool= false;
        console.log(bool);  // false
    }
    
    
    function changeObjToFalse(obj) {
        obj.bool= false;
        console.log(obj.bool);  // false
    }
    
    var obj = {bool : true};
    
    changeToFalse(obj.bool);
    console.log(obj.bool);  // true
    changeObjToFalse(obj);
    console.log(obj.bool); // false
    

    See also same question - two way binding not working with ng-repeat.

提交回复
热议问题