How do you access an angular factory object without using DI that angular provides? For example, I want to have a separate script that adds some server properties to a factory object outside of angular (in a .cshtml file). I don't want to assign the factory object to a separate variable as that would create a global variable.
I'm not sure I understand 100% what you want to achieve, but if I'm not mistaken you want to get a hold of the factory object "outside" of the angular context.
var factoryObject = angular.injector(['nameOfYourModule']).get('factoryname');
Related
I have a controller ctrl which has ctrl.$setValidity() method. I am doing angular.copy(ctrl,ctrlCopy) and I trying to access ctrlCopy.$setValidity() but I got error:
$setValidity is not a function.
I wonder why angular.copy doesn't copy all the $function from an object to another.
You can't angular.copy() a controller... period.
If you want to share a function then create a service that returns the function and inject the service into the two separate controllers
What is the best way to store a "user" object that is accessible across all controllers in an Angular application?
I also have a function that fetches & sets the user.
Option 1: $rootScope
At first I was just setting $rootScope.user = myUser. Then in every control I could use $rootScope. This was fine, until I ended up with a lot of duplicate code since I had to check if it existed at the start of each controller.
// At the start of each controller
if (!$rootScope.user) {
// fetch and set user
}
Option 2: Angular service?
Option 3: Angular factory?
References:
angular.service vs angular.factory
I would use a factory over a service, or $rootScope.
As Ed pointed out below, both a service and a factory are singletons.
$rootScope, as the name implies is the root of all scopes in an angularjs application.
Here is something for reference: http://ilikekillnerds.com/2014/12/angularjs-service-vs-factory/
If you are creating a SPA and not creating a scope without parent then use rootScope it will work like a charm.
When I navigate between one state to another, I want to pass an object to be used in the new state. How can I do this without using global object solutions such as $rootScope? Also I don't want to use the state url parameters.
Basically, I want to do $state.go('myState', {some: 'thing'}) and inside the controller for "myState" I want to be able to reach the passed object somehow.
I assume that by "two states" you mean two different controllers, each of them accessible through different routes.
Angular would recommend to use a service for that as the doc says :
Angular services are substitutable objects that are wired together
using dependency injection (DI). You can use services to organize and
share code across your app.
What do you mean between states? If you mean between controllers, then you could use a shared service to hold the data. Services are singletons, so live for the lifetime of the page.
you can access state parameters in your
.controller('ctrl',['$scope','$stateParams',function($scope , $stateParams ) {
$scope.getParams = function() {
console.log( $stateParams.some );
}
}
https://github.com/angular-ui/ui-router/wiki/URL-Routing#stateparams-service
I am programing in Angularjs, and have have a page that is divided into 3 teplates which one has an html file and js file.
The js file of which template is characterized for having the controller of the html of that template.
At the end i want to pass a variable from a certain controller to another how can i do that once they are in different controllers. Thanks
Mr.Avraam Mavridis, here is some code:
Basically down here is the controller that i want to get the value of the parameter (clientPosition) to pass it to another controller:
crm.controller("clientsModule", ["$scope", "$http", function ($scope, $http {
$scope.changeTemplate = function (index, clientPosition) {
$scope.panelTemplate = index;
$scope.clientPosition = clientPosition;
};
}]);
There are various ways you can achieve this. The easiest way to do this is using a broadcast and watch. You basically set the value in the controller you want to and create a broadcast event for it. In the controller that you would like to access the value, you put a watch and listen to any updates for the value.
The above way is also the dirtiest way to achieve what you want
A more elegant solution would involve create a model binding on the view and accessing those properties via logical progression on the controller you want to access that value in. You essentially create an angular service for your models and then access those services in the controllers.
I'm new to Angular and trying to figure out how to inject some external objects into Angular.
I have a JS script defining a global Payment object with some methods on it. I'd like to somehow import it (the Payment object) into an Angular service (from what I understood the service would be the place to that). How exactly does one go about that?
What I'd like to achieve in the end would be something like:
app.controller("myController", [PaymentService, function (payment) {....}]);
This would work :
app.factory('PaymentService',['$window', function($window){
return $window.Payment;
}]);
Using a factory is an occasion to add additional behavior, for example adding methods or checking that the global object is defined.
You can declare it either as a value:
angular.module('foo').value('PaymentService',window.Payment);
or as a constant:
angular.constant('foo').value('PaymentService',window.Payment);