Search code examples
angularjsangularjs-scope

AngularJS 1.5 call function form JQ, and pass data to component controller function


Guys I also need Your help.How could I call function of my component controller's from JQ function ? And pass data to function controller.I'm really stack on this.

function CalendarDashboardController($scope){


var  week = this;


 week.list = [
    {
    name: 'Sun',
    date: Date.now()

},       {
    name: 'Mon',
    date: Date.now()

},       {
    name: 'Tue',
    date: Date.now()

},       {
    name: 'Wed',
    date: Date.now()

},       {
    name: 'Thu',
    date: Date.now()

},       {
    name: 'Fri',
    date: Date.now()

},       {
    name: 'Sat',
    date: Date.now()

}
]



}


mod.component('calendarDashboard', {
templateUrl:"/src/angular-js/partial/calendarDashboard.html",
bindings: {
startdate: '<',
enddate: '<'
},
  controller: CalendarDashboardController

});

And I have some function in JS it could be various - But this function should call function from my controller (I didn't create it) because, I don't know how to bind event in my JS cod and controller,and how to pass data to this function.


Solution

  • Inside some function outside of angular's scope.

    Event happen and call some function:

    function(){
    
    //Here we take $rootScope of our AngularJS module
    var fj = angular.element(document.body).injector().get('$rootScope')
    
            //And apply broadcast event, in further we will catch it
                fj.$apply(function(){
                    fj.$broadcast("changeDate",startDate)
                        }               );
    
    }
    

    Inside our AngularJS Component Controller:

    var  ownScope = this;
    
    $scope.$on('changeDate', function(events, args){
    
        ownScope.startdate = args; //now we've registered!
    
    
    })
    

    To sum up - It will dynamically change data inside our scope and change it in template in our component.