{"id":1806,"date":"2022-08-30T15:19:36","date_gmt":"2022-08-30T15:19:36","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/angularjs-first-attempt-at-dependency-injection-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:36","modified_gmt":"2022-08-30T15:19:36","slug":"angularjs-first-attempt-at-dependency-injection-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/angularjs-first-attempt-at-dependency-injection-collection-of-common-programming-errors\/","title":{"rendered":"Angularjs first attempt at dependency injection-Collection of common programming errors"},"content":{"rendered":"<p>I have a UserAddController and I want to be able to access a list of countries returned by a Web API. The Web API returns data fine. Here is my app.js where I get the data :<\/p>\n<pre><code>app.factory('Country', function ($resource) {\n    return $resource(\n        \"\/api\/country\/:Id\",\n        { Id: \"@Id\" },\n        { \"update\": { method: \"PUT\" } });  \n});\n<\/code><\/pre>\n<p>This is my Controller :<\/p>\n<pre><code>var UserAddController = function ($scope, $location, service, User) {\n\n    $scope.action = \"Add\";\n    $scope.countries = service.countries;\n\n};\n<\/code><\/pre>\n<p>I am declaring and creating a service here :<\/p>\n<pre><code>app.factory('CountryService', CountryService);\n\nfunction CountryService($resource) {\n    return $resource(\n       \"\/api\/country\/:Id\",\n       { Id: \"@Id\" },\n       { \"update\": { method: \"PUT\" } });  \n}\n<\/code><\/pre>\n<p>I am using the same code as above just for testing purposes. I am injecting this service like this :<\/p>\n<pre><code>UserAddController.$inject = ['$scope', 'CountryService'];\n<\/code><\/pre>\n<p>This is my first attempt at dependency injection and I cannot figure out where I am going wrong. The error I currently get is &#8216;service is undefined&#8217;. I have tried passing both the service and the Country object to the Controller with the same results. Can anybody give any advice?<\/p>\n<p>EDIT : In my Controller, this populates successfully with an alert in the code, but without the alert does not populate. Any reason why this is?<\/p>\n<pre><code>function CountryService($rootScope, $http) {\n    var self = {};\n\n    \/\/self.countries = [{ \"$id\": \"1\", \"CountryId\": 1, \"CountryName\": \"United Kingdom\" }, { \"$id\": \"2\", \"CountryId\": 2, \"CountryName\": \"Republic of Ireland\" }, { \"$id\": \"3\", \"CountryId\": 3, \"CountryName\": \"Australia\" }, { \"$id\": \"4\", \"CountryId\": 4, \"CountryName\": \"New Zealand\" }, { \"$id\": \"5\", \"CountryId\": 5, \"CountryName\": \"United States\" }, { \"$id\": \"6\", \"CountryId\": 6, \"CountryName\": \"France\" }, { \"$id\": \"7\", \"CountryId\": 7, \"CountryName\": \"Germany\" }, { \"$id\": \"8\", \"CountryId\": 8, \"CountryName\": \"Finland\" }];\n\n    $http({\n        method: 'GET',\n        url: '\/api\/country'\n    }).success(function (data, status, headers, config) {\n        self.countries = data;\n    });\n    alert(self.countries);\n    return self;\n}\n<\/code><\/pre>\n<ol>\n<li>\n<p>You need to add other services\/dependencies.<\/p>\n<pre><code>UserAddController.$inject = ['$scope', \n                             '$location', \n                             'CountryService', \n                             'UserService'];\n<\/code><\/pre>\n<p>I have assumed that last dependency is a service with name &#8216;UserService&#8217;. It&#8217;s signature would be<\/p>\n<pre><code>app.factory('UserService', UserService);\n<\/code><\/pre>\n<p>Edit :<\/p>\n<p>You need to instantiate a new variable.<\/p>\n<pre><code>\/\/Inside function body\n$scope.countries = service.countries;\n$scope.newCountry = $scope.countries.get({Id : someId},callbackFn);\n<\/code><\/pre>\n<p>Now you have a counrtry with &#8216;someId&#8217; in $scope.newCountry<\/p>\n<\/li>\n<li>\n<p>Make sure you injected ngResource.<\/p>\n<pre><code>app = angular.module(\"app\", ['ngResource']);\n<\/code><\/pre>\n<p>You need to inject the modules correcly<\/p>\n<pre><code>UserAddController.$inject = ['$scope', '$location', 'CountryService', 'user'];\n<\/code><\/pre>\n<p>This is quoted the doc.<\/p>\n<blockquote>\n<p>You can specify the service name by using the $inject property, which is an array containing strings with names of services to be injected. The name <strong>must<\/strong> match the corresponding service ID registered with angular. <strong>The order of the service IDs matters<\/strong>: the order of the services in the array will be used when calling the factory function with injected parameters.<\/p>\n<\/blockquote>\n<p>I created a FIDDLE and you can try.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 01:37:25. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a UserAddController and I want to be able to access a list of countries returned by a Web API. The Web API returns data fine. Here is my app.js where I get the data : app.factory(&#8216;Country&#8217;, function ($resource) { return $resource( &#8220;\/api\/country\/:Id&#8221;, { Id: &#8220;@Id&#8221; }, { &#8220;update&#8221;: { method: &#8220;PUT&#8221; } }); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1806","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1806","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1806"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1806\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1806"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1806"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1806"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}