0

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;
        }
    });
}
EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
  • 1
    _id_ is global var here, so you can use it directly – Grundy Apr 30 '15 at 12:36
  • Yeah i tried but it showing undefined. I want to use this id in angular controller that is created for this same cshtml page – Nikita Sharma Apr 30 '15 at 12:38
  • it can be _undefined_ before you click, but after if `events.length` it should be not undefined – Grundy Apr 30 '15 at 12:42
  • This is bad design. Why aren't you just porting your code into angular? Should definitely read: [thinking-in-angularjs-if-i-have-a-jquery-background](http://stackoverflow.com/questions/14994391/thinking-in-angularjs-if-i-have-a-jquery-background) – charlietfl Apr 30 '15 at 13:36

1 Answers1

0

@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 (I.e outside a scope)?

Best of luck

Community
  • 1
  • 1
Chintana Meegamarachchi
  • 1,798
  • 1
  • 12
  • 10