jazzoria jazzoria - 1 year ago 101
AngularJS Question

angularjs using factory and controller

Im new to angularjs. How to use a controller to access the values that are assigned inside


var app = angular.module('myApp', []);

app.factory('testFactory', function(){

var alpha = {};


enter code here

return alpha;

So here I want to access and display apple orange and grape in my view.

Answer Source

You probably don't need a factory, you can define your sample data directly in the controller. Here's a quick plunker with both.


var app = angular.module('myApp', []);

app.factory('testFactory', function() {
    var alpha = {};
    alpha.sample = ['apple', 'orange', 'grape'];
    return alpha;

app.controller('testController', ['testFactory', function(testFactory){
    var vm = this;
    vm.data = testFactory.sample;


<html ng-app="myApp">
       <script src="app.js"></script>
    <body ng-controller="testController as controller">
            <li ng-repeat="fruit in controller.data" ng-bind="fruit"></li>


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download