angularjs - Unable to inject service in the controller -
i have done when service , controller in single file (usually, app.js) , works without issues.
now, have controllers , services organized in different files , respective folders. need inject 1 particular service in controller running error.
this simple angular app working on has way manage youtube favorite videos. getting error on add page. click on add button see error.
here have far:
index.html
<!doctype html> <html ng-app="youtube-favorites"> <head> <title>my youtube favorites</title> <link rel="stylesheet" type="text/css" href="bower_components/bootstrap/dist/css/bootstrap.min.css" /> <link rel="stylesheet" type="text/css" href="css/custom.css" /> </head> <body> <div class="container"> <div class="row"> <div class="col-md-12" style="border: 1px solid red;"> <div ui-view></div> </div> </div> </div> <script src="bower_components/angular/angular.js"></script> <script src="bower_components/angular-bootstrap/ui-bootstrap-tpls.min.js"></script> <script src="bower_components/angular-ui-router/release/angular-ui-router.min.js"></script> <script src="app.js"></script> <script src="services/add.js"></script> <script src="controllers/home.js"></script> <script src="controllers/add.js"></script> </body> </html> app.js
angular.module('youtube-favorites', ['ui.bootstrap', 'ui.router']) .config(['$stateprovider', '$urlrouterprovider', function($stateprovider, $urlrouterprovider) { // unmatched url, redirect /home $urlrouterprovider.otherwise('/home'); // set states $stateprovider .state('home', { url: '/home', templateurl: 'views/home.html', controller: 'homecontroller' }) .state('add', { url: '/add', templateurl: 'views/add.html', controller: 'addcontroller' }); } ]); controller - add.js
angular.module('youtube-favorites') .controller('addcontroller', ['$scope', '$log', 'addservice', function($scope, $log, addservice) { $log.info('add'); $scope.addfavorite = function() { addservice.addfavorite($scope.title, $scope.youtubeurl) $log.info('title', $scope.title); $log.info('youtubeurl', $scope.youtubeurl); }; } ]); service - add.js
angular.module('youtube-favorites', []) .service('addservice', ['$scope', '$log', '$q', '$window', function($scope, $log, $q, $window) { $log.info('add service called'); this.addfavorite = function(title, url) { $log.info('title', title); $log.info('url', url); $window.localstorage.setitem('youtubefavorites', angular.tojson({ title: title, url: url })); return true; }; } ]); view - add.html
<form name="addform" ng-submit='addfavorite()' novalidate> <div class="form-group"> <label for="title">title</label> <input type="text" name="title" class="form-control" placeholder="awesome video!" aria-describedby="title" ng-model="title" required /> <!-- <p ng-show="addform.title.$error.required" class="help-block">please enter title.</p> --> </div> <div class="form-group"> <label for="youtubeurl">youtube <em>embed</em> url</label> <input type="text" name="youtubeurl" class="form-control" placeholder="https://www.youtube.com/embed/someid" aria-describedby="youtubeurl" ng-model="youtubeurl" required /> <!-- <p ng-show="addform.youtubeurl.$error.required" class="help-block">please enter url.</p> --> </div> <div class="form-group"> <button class="btn btn-default grey" type="submit">submit</button> </div> </form> here error getting:
pretty sure not injecting service correctly.
i did try adding service dependency app module per below, still giving error.
angular.module('youtube-favorites', ['ui.bootstrap', 'ui.router', 'youtube-favorites.addservice']).... how fix this?
in addservice, change this:
angular.module('youtube-favorites', []) to this:
angular.module('youtube-favorites') you creating new module same name when include empty [].
also, should passing in $rootscope service because $scope is available controllers.

Comments
Post a Comment