javascript - Getting error when pass data between controllers using sample service -
as part of angular js learning, created small app pass data between 2 controllers using services..below code pass data between 2 controllers..
controller code
<!doctype html> <html lang="en-us" ng-app="myapp"> <head> <title></title> <script src="https://code.angularjs.org/1.5.8/angular.min.js"></script> <script type="text/javascript"> var app = angular.module('myapp', []); app.service('sampleservice', [function () { this.user = [name = 'pra', empno ='1234'] this.designation = 'teamlead'; }]) app.controller('namectrl', ['$scope','$sampleservice', function ($scope,$sampleservice) { $scope.empname = $sampleservice.user.name; $scope.empnumber = $sampleservice.user.empno; $scope.empdesignation = $sampleservice.designation; $scope.changevals = function(){ $sampleservice.user.empno = '9876'; $sampleservice.designation = 'manager'; $scope.empnumber = $sampleservice.user.empno; $scope.empdesignation = $sampleservice.designation; } }]) app.controller('namectrl1', ['$scope','$sampleservice', function ($scope) { $scope.uempiid = $sampleservice.user.empno; $scope.uempdesignation = $sampleservice.designation; $scope.updatevals = function(){ $scope.uempiid = $sampleservice.user.empno; $scope.uempdesignation = $sampleservice.designation; } }]) </script> </head>
below code html code
<body> <div ng-controller="namectrl"> <div><b> details - controller 1</b></div> <p>name : {{empname}}</p> <p>location : {{empnumber}}</p> <p>designation : {{empdesignation}}</p> <input type="button" value="change values" ng-click="changevals()" /> </div> <br /> <div ng-controller="namectrl1"> <div><b>details - controller 2</b></div> <input type="button" value="change values" ng-click="updatevals()" /> <p>location : {{uempiid}}</p> <p>designation : {{uempdesignation}}</p> </div> </body>
i not able display details , getting error
angular.js:13920 error: [$injector:unpr] http://errors.angularjs.org/1.5.8/$injector/unpr?p0=%24sampleserviceprovider%20%3c-%20%24sampleservice%20%3c-%20namectrl @ error (native)
could 1 please point me in right direction on doing wrong code ..
many in advance....
don't use '$' while injecting service in controller:
change:
app.controller('namectrl', ['$scope','$sampleservice', function ($scope,$sampleservice) {
to:
app.controller('namectrl', ['$scope','sampleservice', function ($scope,sampleservice) {
edit
also (as @estus suggested), service undefined in namectrl1
change code of namectrl2
from
app.controller('namectrl1', ['$scope','$sampleservice', function ($scope) {
to
app.controller('namectrl1', ['$scope','sampleservice', function ($scope, sampleservice) {
in code, whenever you're using sampleservice, dont use sampleservice
instead of $sampleservice
.
edit 2
change
app.service('sampleservice', [function () { this.user = [name = 'pra', empno ='1234'] this.designation = 'teamlead'; }])
to
app.service('sampleservice', [function() { this.user = {name : 'pra', empno : '1234'}; this.designation = 'teamlead'; }])
final code this:
<!doctype html> <html lang="en-us" ng-app="myapp"> <head> <title></title> <script src="https://code.angularjs.org/1.5.8/angular.min.js"></script> <script type="text/javascript"> var app = angular.module('myapp', []); app.service('sampleservice', [function() { this.user = {name : 'pra', empno : '1234'}; this.designation = 'teamlead'; }]) app.controller('namectrl', ['$scope', 'sampleservice', function($scope, sampleservice) { $scope.empname = sampleservice.user.name; $scope.empnumber = sampleservice.user.empno; $scope.empdesignation = sampleservice.designation; $scope.changevals = function() { sampleservice.user.empno = '9876'; sampleservice.designation = 'manager'; $scope.empnumber = sampleservice.user.empno; $scope.empdesignation = sampleservice.designation; } }]) app.controller('namectrl1', ['$scope', 'sampleservice', function($scope, sampleservice) { $scope.uempiid = sampleservice.user.empno; $scope.uempdesignation = sampleservice.designation; $scope.updatevals = function() { $scope.uempiid = sampleservice.user.empno; $scope.uempdesignation = sampleservice.designation; } }]) </script> </head> <body> <div ng-controller="namectrl"> <div><b> details - controller 1</b> </div> <p>name : {{empname}}</p> <p>location : {{empnumber}}</p> <p>designation : {{empdesignation}}</p> <input type="button" value="change values" ng-click="changevals()" /> </div> <br /> <div ng-controller="namectrl1"> <div><b>details - controller 2</b> </div> <input type="button" value="change values" ng-click="updatevals()" /> <p>location : {{uempiid}}</p> <p>designation : {{uempdesignation}}</p> </div> </body> </html>
Comments
Post a Comment