Here is the code for my service. It is taken almost exactly from the first answer here.
var myApp = angular.module('myApp', ['ngRoute']);
myApp.service('sharedProperties',function(){
var property = "First";
return{
getProperty:function(){
return property;
},
setProperty:function(value){
property = value;
}
};
});
Here is the JSFiddle that shows my code in full and it not working.
The error I get is:
"Error: sharedProperties is not defined
For the line that the alert is on. I am just using an alert as a mere example of showing that the service is working before I extend the code further.
Anyone know why this simple example of a service is not working? I've thoroughly went over the code to make sure there are no typos or anything silly like that.
The answer that I linked has a JSFIDDLE that uses an older version of AngularJS. I was able to replace it with the version Angular being used in my JSFIDDLE and it still worked fine so it doesn't seem to be a version issue.
You need to inject the service to your controller:
myApp.controller('mainController', function($scope, sharedProperties) {
(Minification safe syntax)
myApp.controller('mainController', ["$scope", "sharedProperties", function($scope, sharedProperties) {
Working fiddle: http://jsfiddle.net/b2fCE/733/
You need to inject the service in your controller.
Here is the fiddle https://jsfiddle.net/b2fCE/732/
myApp.controller('mainController', function($scope, sharedProperties) {
// create a message to display in our view
$scope.message = 'Everyone come and see how good I look!';
alert(sharedProperties.getProperty());
});
Related
got to work with angular JS on learning how angular works.
I am trying controllers, directvies and services in an example project:
[http://codepen.io/anon/pen/PpOERy][1]
Actually I got this error if I try to use my service on clicking the button:
TypeError: Cannot read property '*****' of undefined
Whats my failure?
Regards n00n
You forgot to annotate your service, little typo ;)
Your code
myApp.controller('MainCtrl', ['$scope', function ($scope, CalculatorService) {
Correct code
myApp.controller('MainCtrl', ['$scope', 'CalculatorService', function ($scope, CalculatorService) {
This is a general dependency injection question. Obviously I am doing it wrong.
I am trying to get angular-xeditable working in my app.
https://vitalets.github.io/angular-xeditable/
I've installed it using bower, and added the appropriate script link to my head.
Now I'm trying to inject it.
The docs say
var app = angular.module("app", ["xeditable"]);
so, in my app: I do this:
portalApp.controller('portalController',
['$scope', '$http','$filter', 'xeditable',
function($scope, $http, $filter, xeditable) {
but I get the provide error, meaning it can't find xeditable.
angular.js:13642Error: [$injector:unpr] http://errors.angularjs.org/1.5.6/$injector/unpr?p0=xeditableProvider%20%3C-%20xeditable%20%3C-%20portalController
What am I doing wrong?
OK, duh. It should be
var portalApp = angular.module("portalApp", ['xeditable']);
portalApp.controller('portalController', ['$scope', '$http','$filter', function($scope, $http, $filter) {
I'm still getting lots of errors, but at least not that one.
I'm new to AngularJs and I try to understand the basics of controller binding. lets say I have this in my html page:
<div ng-controller ="MainController">
<h2>{{message}}</h2>
</div>
Now, in my script file I have two options(as I saw in different tutorials):
1:
var myApp = angular.module('myApp',[]);
myApp.controller('MainController', ['$scope', function($scope) {
$scope.message = 'hello';
}]);
2:
var MainController = function($scope)
{
#scope.message = "Hello";
};
Offcourse I prefer the second approach however only the first option works for me. I'm working with AngularJS 1.4.4, could it be the second approach is now deprecated?
Thanks!
The second approach is just assigning the controller code to a variable, where in your first approach you pass it directly into the controller function. To make the second approach work, you'd have to pass that variable into the controller function as follows.
myApp.controller('MainController', MainController);
You're doing the same thing. I go with the second approach because I think it's cleaner. Check out John Papa's style guide for clean guidelines on how to write your Angular code.
In a recent question, I was having an issue with a simple modal dialog implemented using Angular UI for Bootstrap.
I started with this fiddle, and the person who answered came up with this result.
However, one thing immediately caught my attention!
Old Controller Implementation
var controllers = angular.module('app.controllers', []);
controllers.controller('ModalController', ['$scope', '$modal', '$log',
function ($scope, $modal, $log) {
// Overarching controller code...
}
]);
controllers.controller('ModalInstanceController', ['$scope', '$modalInstance',
function ($scope, $modalInstance, params) {
// ...Modal Instance Code...
}
]);
This code does not work with the Angular UI for Bootstrap Modal, but for some reason, this code does:
var ModalController = function($scope, $modal, $log) {
// Overarching controller code...
};
var ModalInstanceController = function($scope, $modalInstance, params) {
// Modal Instance Code...
};
...The problem being, that AngularJS code is usually modularized like the first example to avoid cluttering the global namespace.
So far none of my experiments have been able to get a modularized setup to succeed in the first place. I attempted some simple substitutions, where I would make one controller or the other be a modularized controller, in hopes that it was only one controller preventing the params from being passed between controllers; this turned out not to be the case. Implementing $scope.params = []; before declaring the $scope.open() function, and populating $scope.params in the open function similarly had no effect.
Question: In the context of the AngularUI for Bootstrap system, why does the modularized approach fail? And more importantly, how can I make it work?
Here is your fixed plnkr (http://jsfiddle.net/pEmXt/4/), it had several problems:
You defined your modules in the wrong order.
You had the ui DI in the wrong place.
Your resolve syntax was wrong.
The DI in your modal instance controller was missing an item in the list of dependencies.
The resolve method is used like this:
resolve: {
objectName: function(){
return myObject;
}
}
OK...so I recently ran into same issue and was perplexed also. I just never bothered to dig into it. A quick trip to angular-ui github repo and I found out in the issue tracker.
Angular-Ui demos are passing a function reference as controller. For a modular controller it needs to be a string
var modalInstance = $modal.open({
templateUrl: 'myModalContent.html',
controller: 'ModalInstanceCtrl', /* use string for modular controller */
/* OR */
controller: ModalInstanceCtrl, /* use reference for controller as function*/
});
Issue tracker reference: https://github.com/angular-ui/bootstrap/issues/2330
Working demo from issue tracker: http://plnkr.co/edit/38vBcPalBBNMgYis4cZX?p=preview
In the first example, the module 'app.controllers' has to be added to the list of dependencies for the main app.
var app = angular.module('app', ['app.controllers']);
In the second instance the controllers are global functions and therefore are visible without being explicitly added as a dependency.
I've been able to use the following to get my jSON which is fine but I've been told that using a Factory and Service would be a better option. For the life of me I'm unable to get anything working and I can't even see why I should use a Factory.
Could someone advise why or at least help me out at all? My main requirement here is to get the jSON and then pass it through into an <li></li> list for each object (I haven't tried to get this working yet).
theApp.controller('MenuSideController', ['$scope','$http', function ($scope, $http){
$http.get('/directory/assets/inc/category.php').success(function(data) {
$scope.list = data;
});
}]);
What if you wanted to make the same server call in another controller. Would you copy&paste the same $http.get(... code? No, of course not, because that would be a really bad code smell and a prime example of DRY (Don't Repeat Yourself) violation.
This is why your $http needs to go into a separate function (call it "service" or "factory"), which you will be able to call from anywhere.
theApp.controller('MenuSideController', [
'$scope',
'CategoryService',
function ($scope, CategoryService){
CategoryService.getList()
.success(function(list){
$scope.list = list;
});
}
]);
theApp.factory('CategoryService', [
'$http',
function($http){
return {
getList: function(){
return $http.get('/directory/assets/inc/category.php');
}
};
}
]);