Thursday, 15 August 2013

angularjs - Passing string to ng-click -



angularjs - Passing string to ng-click -

i've trying working out no success far.

the view:

<div class="col-md-4 col-xs-4 col-sm-4"> <div ng-click="action()" class="nav-link">click me action in scope </div> </div>

what trying replace "action()" variable send service modifies rootscope.

in service:

$scope.myaction = function() { $scope.$broadcast("actionevent"); }

what achieve:

<div class="col-md-4 col-xs-4 col-sm-4"> <div ng-click="myaction()" class="nav-link">click me action in scope </div> </div>

but when seek replace it, ng-click not recognize action. text displayed fine, action not execute.

any thought how possible add together action rootscope executes in ng-click?

looking forwards answers, give thanks !

btw, i've tried these options:

1) angular.js parsing value in ng-click 2) angular js ng-click action function string

if function needs access rootscope, don't need scope function within service. inject rootscope controller utilize it.

module.controller("myctrl", function($scope, $rootscope){ $scope.myaction = function() { $rootscope.$broadcast("actionevent"); }; });

if function service, create function fellow member of service, utilize service in ng-click.

module.service("myservice", function($rootscope){ this.myaction = function() { $rootscope.$broadcast("actionevent"); // other stuff }; });

in controller

module.controller("myctrl", function($scope, myservice){ $scope.ms = myservice; });

in markup

<div ng-click="ms.myaction()" class="nav-link">click me action in scope </div>

note doesn't create sense inject $scope service, doesn't have scope. $rootscope different since service, service can depend on. note shouldn't $scope.myaction = myservice.myaction might cause unexpected behavior.

angularjs rootscope

No comments:

Post a Comment