How to copy an angular $ function from one object to another - angularjs

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

Related

How can I call a function from controller1 in controller2? AngularJS

I have a function defined in controller1 and would like to use it in controller2. When I do $scope.functionName() or even $rootScope.functionName() I get this error
TypeError: $scope.functionName() is not a function
It works absolutely fine in controller1 but as expected not in controller2. I do not want to copy and paste an existing function into controller2 to use it. It should be reusable.
Create a service or a factory and put the function in there. Next, make both controllers use the service/factory by injecting it to both controllers.
Use events($on, $broadcast and $emit) to communicate btn two controllers.
Syntax:
$scope.$broadcast("MyEvent",data);
$scope.$emit("MyEvent",data);
$scope.$on("MyEvent", function(evt,data){
// handler code here });
Hope it helps.

Pass a variable from controllers

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.

Add existing JS object to Angular app as a dependency

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);

Controller Instance Angularjs

I am facing a different problem.
According to my code I have a common service where I have written all my functions. I have injected service into 2 different controllers.
Here my Problem is I want to call the function in 2nd controller when common service executing under 1st controller. I am passing $scope variable to common service. so when service working under 1st controller $scope variable refers 1st controller only But I want to change the reference of $scope to 2nd controller..
Thanks

Accessing an angular factory object without DI

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');

Resources