javascript - Access Data from Factory in angular -
defining module
var app = angular.module("sachin", ["ng-fusioncharts"]); //making service app.service('team',function(){ var self = { 'runs_aus':0 }; return self; });
making factory
app.factory('taskfactory',function($scope,team){ $scope.mydatasource = { chart: { caption: "runs opposition", subcaption: "runs against each team", numberprefix: "", theme: "ocean" }, data:[{ label: "australia", value: self.runs_aus }, { label: "pakistan", value: "7300" }, { label: "south africa", value: "5900" }, { label: "west indies", value: "5200" }, { label: "england", value: "3300" }] }; return mydatasource; });
injecting factory , service in controller
app.controller("myctrl", function($scope,taskfactory,team){ console.log(taskfactory.mydatasource); //can't access data on here }
/*
all want access mydatasource variable in controller
how can that?
thanks in advance
$scope used expose variables view shouldn't use $scope in factory. achieve want, can follow following code sample:
var test = angular.module("test",[]); test.service("team", function(){ var self = { 'runs_aus':0 }; return self; }); test.factory('taskfactory',function(team){ var mydatasource = { chart: { caption: "runs opposition", subcaption: "runs against each team", numberprefix: "", theme: "ocean" }, data:[{ label: "australia", value: team.runs_aus }, { label: "pakistan", value: "7300" }, { label: "south africa", value: "5900" }, { label: "west indies", value: "5200" }, { label: "england", value: "3300" }] }; return mydatasource; }); test.controller("myctrl", function($scope,taskfactory){ console.log(taskfactory); //can't access data on here });
Comments
Post a Comment