简体   繁体   中英

How can I get the ID from a javascript function to an angularJS controller

I have this javascript function on a cshtml page and want to access the value of id from this function to on angular controller of this page. How can I access?

 var id;
function scheduler_change(e) {
    var view = this.view().element;
    view.off("click").on("click", ".k-event", function () {
        var events = e.events;
        if (events.length) {
             id = events[events.length - 1].appointmentID;
            var cat = events[events.length - 1].category;
            var subject = events[events.length - 1].title;
            var cuid = events[events.length - 1].cuid;
        }
    });
}

@Grundy is correct. As Id is a global variable you should be able to access it from angular. But the issue is when scheduler_change is fired, and event.length has a value, Id will get updated. But Angular will have no way of knowing this!!!.

This is due to the fact Angulars change detection happens within angular's boundaries and Id, as it is now, is out of the boundary.

One solution you may have is to use an observable objects. Mind you this is not yet widely supported. Following post discusses rollowing your own observable. It may be of some help.

Angular: Is it possible to watch a global variable (Ie outside a scope)?

Best of luck

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