简体   繁体   中英

Code Understanding

I am trying to understand following piece of code:

    function changeInfo(newCallInfo) {
    angular.extend(callInfo, newCallInfo);
    internalScope.$emit('changed', angular.copy(callInfo));
}

function observeInfo(fn) {
    return internalScope.$on('changed', function (e, val) {
        fn(val);
    });
}

As per my understanding $emit behave same like $broadcast here so we emit 'change' and then catch this event in observeInfo() but what is the benefit to send angular.copy(callInfo) parameter as function and what exactly observeInfo(fn) do with this parameter . Can someone explain this

The observeInfo function is simply providing a way to define a callback that executes every time the "changed" event is emitted. This function gets called with the value provided with the event as a parameter. In this example, it is the callInfo variable.

The angular.copy() is simply used to ensure that the same object (or reference to that object) is not passed into the callback, so any changes that might be made to the data within the callback function will not affect the original variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM