I think I've found the solution to my own question.
Change the 'test' factory to this:
myApp.factory('test', ['$q', function($q) {
var $this = angular.extend(this, {
one: one
});
function one() {
return 'one';
}
return $this;
}]
);
In the controller change:
console.log(test.one);
To this:
console.log(test.one());
And now the codebehaves as expected.
Thanks
On Friday, November 21, 2014 2:38:44 PM UTC, Mike Clarke wrote:
>
> Hello,
>
> I am just starting out with AngularjS and I have got the following example
> code:
>
>
>
>
> *test.js'use strict';myApp.factory('test', ['$q', function($q) {
> var $this = function() { angular.extend(this, {
> one: one }); };
> function one() { return 'one';
> } return $this; }]);*
> *controllers.js*
>
> 'use strict';
>
> myApp.controller('TestController', ['$scope', 'test', function($scope,
> test) {
> console.log(test.one);
> }]
> );
>
> *modules.js*
>
> 'use strict';
>
> var myApp = angular.module('myApp', ['ngRoute']);
>
> *demo.html*
>
> <html ng-app="myApp">
> <head>
> <script src="
> http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js"
> ></script>
> <script src="
> http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-route.min.js
> "></script>
> <script src="./modules.js"></script>
> <script src="./test.js"></script>
> <script src="./controllers.js"></script>
> <title>AngularJS Demo</title>
> </head>
> <body ng-controller="TestController">
> <h1>AngularJS Demo</h1>
> </body>
> </html>
>
> I don't know what test.one is being output as undefined to the console.
>
> I was expecting '*one*' to be displayed as this is the result of the
> function call.
>
> Am I doing something wrong here?
>
> Thanks in advance.
>
>
>
--
You received this message because you are subscribed to the Google Groups
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.