angularjs - Angular Factory Injection not working -
im trying angular work @ basic level. dont it. im doing tutorial , everything. kills me.
here im doing:
1.) html
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>document</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.min.js"></script> <script src="js/app.js"></script> <script src="js/controller.js"></script> <script src="js/friendsfactory.js"></script> </head> <body> <div ng-app="friendsapp"> <div ng-controller="friendscontroller"> <h3>{{girlfriendname}}</h3> </div> </div> </body> </html>
2.) app.js
var friendsapp = angular.module('friendsapp',[]);
3.) controller.js
friendsapp.controller('friendscontroller', ['$scope','friendsfactory', function($scope, friendsfactory){ $scope.girlfriendname = friendsfactory.girlfriend(); }]);
4.) friendsfactory.js
friendsapp.factory('friendsfactory', function (){ this.girlfriend = function(){ var name = "girlfriends name"; return name; } });
i wanted try @ basic level have spent 5 hours trying name factory controller. if write name manually controller works, controller called.
can tell me thinking wrong here?
thank much!
modify factory code:
friendsapp.factory('friendsfactory', function (){ this.girlfriend = function(){ var name = "girlfriends name"; return name; } });
to:
friendsapp.factory('friendsfactory', function (){ function girlfriend(){ var name = "girlfriends name"; return name; } return { girlfriend: girlfriend } });
Comments
Post a Comment