1

Attempting to learn angularjs just because but not getting the basics. Why doesn't this work? 'hello' is bound and will display "working" if uncommented.

angular.module('myApp', [])
.service('formService', function () {
     this.names = ['John', 'James', 'Jake'];
}).controller('FormCtrl', ['$scope', function ($scope, formService) {
    $scope.hello = formService.names[1];//"working";
}]);

[html fragment]

<div ng-app="myApp" ng-controller="FormCtrl">
{{hello}}
</div>

2 Answers 2

5

You've left of the string version of your service off. So change:

.controller('FormCtrl', ['$scope', function ($scope, formService)

to

.controller('FormCtrl', ['$scope','formService', function ($scope, formService)

Or because this is such a common, easy mistake to make some people use ngMin.

Then you can use the form: .controller('FormCtrl', function ($scope, formService) and let ngMin (for example in your build script) handle converting it to the min-safe form you used.

Sign up to request clarification or add additional context in comments.

Comments

1

You're missing the formservice parameter in your controller function, you should see an error in the console. Everything else looks right.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.