javascript - Passing Parameter to Angular Factory from controller -
i couldn't pass parameter angular controller factory. can 1 me on this? works without passing parameter when pass it's not.
var app = angular.module('employee', ['ui.grid', 'ui.grid.savestate', 'ui.grid.selection', 'ui.grid.cellnav', 'ui.grid.resizecolumns', 'ui.grid.movecolumns', 'ui.grid.pinning', 'ui.bootstrap', 'ui.grid.autoresize','ui.grid.pagination']); app.controller('empctrl', ['$scope', '$http', '$interval', '$modal', '$log', 'gridservice', function ($scope, $http, $interval, $modal, $log, gridservice) { $scope.loadnextpage = gridservice.loadnextpage("5"); }]); var gridservice = function ($http, $rootscope) { return { loadnextpage: function (hh) { alert(hh); }, gridoptions:gridoptions }; }; app.factory('gridservice', ['$http', '$rootscope', gridservice]);
and how use in view
<span id="pcnext" class="glyphicon glyphicon-step-forward" ng-click="loadnextpage()"> </span>
the problem in controller:
$scope.loadnextpage = gridservice.loadnextpage("5");
this means loadnextpage
not function rather result of call function in service. btw doesn't return rather displays alert. in view, you're using loadnextpage
function call...
change controller's loadnextpage
function can call view.
$scope.loadnextpage = gridservice.loadnextpage;
and in view:
<span id="pcnext" class="glyphicon glyphicon-step-forward" ng-click="loadnextpage(5)"> </span>
this should work.
note: suspect
gridoptions
defined somewhere outside of scope of code provided in question doesn't throw , error because of missing (likely) object. considered typo in code , not actual problem.
don't want params in view?
no problem. can either create wrapper function or bind specific parameters in code:
// wrap $scope.loadnextpage = function() { return gridservice.loadnextpage("5"); }; // bind $scope.loadnextpage = gridservice.loadnextpage.bind(this, 5);
or bake number in service...
Comments
Post a Comment