AngularJS function to another file - angularjs

I have an AngularJS script (to be run on the web) and a couple of specific functions are starting to become very long, long enough that I've built a spreadsheet to generate all of the different cases, which I then simply paste into the code.
(function(){
var app = angular
.module('app',[])
.controller('HostController',HostController);
HostController.$inject = ['$scope'];
function HostController($scope){
var host = this;
host.variable = "some text";
function reallyLongFunction(a,b) {
switch(a) {
case "something":
switch(b) {};
break;
case "something else":
switch(b) {};
break;
};
}
}
})();
I want to move them out of the main file so that it's not cluttered with these long, generated functions while I work on the rest of the programme.
I could simply move the functions directly into another file, but they need to access variables of the type host.variable, and so need to be in the scope of the main Angular app, I believe?
How can I move these functions into a different file while retaining their access to these variables?

You can move your method to separate file by creating an angular service as well. Inject that service in your controller and access the method like someSvcHelper.reallyLongFunction(a,b). This aproach will also make this method of yours as generic and will be available for other controllers as well.
But in this case you will have to pass the variables required by this function as arguments.

Using nested ng-controller's you can have access to the other controller scope in the $scope.
angular.module('app', [])
.controller('ctrl', function() {
var vm = this;
vm.value = 1;
})
.controller('auxCtrl', function($scope) {
var aux = this;
aux.result = function() {
return $scope.vm.value + 5;
}
});
<div ng-app="app" ng-controller="ctrl as vm">
<div ng-controller="auxCtrl as aux">
<input type="number" ng-model="vm.value" /> <br/>
{{vm.value}} <br/>
{{aux.result()}}
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>
Edit: What if i need more than one controller?
Well, in this case i think that nested controllers will be really cumbersome, so you can try a service that has an instance of the controllers scope.
angular.module('app', [])
.service('greeter', function() {
const self = this;
self.scope = {};
self.use = scope => self.scope = scope;
self.greet = () => 'Hello, ' + self.scope.myName;
})
.service('fareweller', function() {
const self = this;
self.scope = {};
self.use = scope => self.scope = scope;
self.farewell = () => 'Goodbye, ' + self.scope.myName;
})
.controller('ctrl', function($scope, greeter, fareweller) {
$scope.myName = 'Lorem Ipsum';
$scope.greeter = greeter;
$scope.fareweller = fareweller;
greeter.use($scope);
fareweller.use($scope);
});
<div ng-app="app" ng-controller="ctrl">
<input type="text" ng-model="myName"> <br>
{{greeter.greet()}} <br>
{{fareweller.farewell()}} <br>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.2/angular.min.js"></script>

Related

Passing Object from One Controller to Another AngularJS

I need to pass an object from one controller to another and have used this solution but it is not working.
Here the code:
angular.module("customerApp", [])
.controller('MainCtrl', function ($scope, myService, $http, $location) {
var vm = this;
vm.pinFormCheck = function () {
vm.count++;
if (vm.pinForm.$valid && vm.details.PIN === vm.pin && vm.count <= 2) {
location.href = "http://localhost:51701/Home/MainMenu";
$scope.obj = {
'cid': 'vm.details.CID',
'name': 'vm.details.Name',
'pin': 'vm.details.PIN',
'bal': 'vm.details.Bal',
'status': 'vm.details.cardStatus'
};
console.log(vm.details.Bal);//the correct balance get displayed in console
} else {
vm.failPin = true;
}
};
})
.controller('CheckCtrl', function ($scope, myService) {
$scope.data = myService.getObj();
})
.factory('myService', function () {
var obj = null;
return {
getObj: function () {
return obj;
},
setObj: function (value) {
obj = value;
}
}
});
Here is the view from which the first object is passed:
<body ng-app="customerApp">
<div ng-controller="MainCtrl as vm">
<form name="vm.pinForm">
<input type="password" ng-model="vm.pin" ng-required="true" />
<p><button ng-disabled="vm.count >=3" ng-click="vm.pinFormCheck();" ng-init="vm.count=0">Proceed</button></p>
</form>
...
Here' the second view where I need the object
<html ng-app="customerApp">
<body ng-controller="CheckCtrl">
<div>
<h1>your balance is {{data.bal}}</h1>
....
The balance from vm.details.Bal from the first view must appear in data.bal in the second view, but nothing is appearing.
You can just save vm.details in some factory.
And then get it in CheckCtrl from this factory.
Factories in AngularJS implement singleton pattern. So saved data will be kept in until your app exists.
You tried to do next thing myService.getObj(); But you didn't save anything to the service.
Inject myService to the MainCtrl and then save details into it.

Can you reuse $scope.$watch

I found some code I want to copy / paste and use in two controllers. It watches something.
$scope.$watch('thing', function (thing) {
// do cool stuff with thing
}
Instead of copy/paste, I'd like to put it in a service and use the service from both controllers sortof like this:
angular.module('myApp')
.factory('CoolService',
function () {
$scope.$watch('thing', function (thing) {
// do cool stuff with thing
}
}
Now if I do this, it won't know what $scope is, right? (According to some reading, it won't let me do that anyway.)
Nevertheless, I'd like to say, If you have this service, you get this watch.
There's a hint I can do this: Passing current scope to an AngularJS Service
So I took his example, fixed it, and scope.watch works in there, but now I can't set other scope variables inside the watch. I just don't know enough javascript to do it, but I'm close. I really think it will work with the right syntax...
angular.module('blah', []);
angular.module('blah').factory('BlahService', function() {
//constructor
function BlahService(scope) {
this._scope = scope;
this.myFunc = function(){
this._scope.otherVar = this._scope.someVar;
};
this._scope.$watch('someVar', function(someVar) {
// do cool stuff with thing
_scope.otherVar = this._scope.someVar; // undefined
this._scope.otherVar = this._scope.someVar; // undefined
this.myFunc(); // undefined
BlahService.prototype._someFunction(); // works, but...
return someVar;
});
}
//wherever you'd reference the scope
BlahService.prototype._someFunction = function() {
if (this._scope['someVar'] == 1) // undefined
this._scope['someVar']++;
}
return BlahService;
});
angular.module('blah').controller('BlahCtrl', function($scope, BlahService) {
$scope.someVar = 4;
$scope.BlahService = new BlahService($scope);
});
angular.module('blah').controller('Blah2Ctrl', function($scope, BlahService) {
$scope.someVar = 6;
$scope.BlahService = new BlahService($scope);
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<html ng-app="blah">
<body>
<div ng-controller="BlahCtrl">
1a. <input ng-model="someVar">
1b. <input ng-model="otherVar">
</div>
<div ng-controller="Blah2Ctrl">
2. <input ng-model="someVar">
2b. <input ng-model="otherVar">
</div>
</body>
</html>
The key feature that this snippet has is that the scopes are different scopes. It doesn't act like a singleton.
Passing $scopes to a service sounds like a recipe for memory leaks. If nothing else it's the long way around.
Instead consider just doing this in each directive:
scope.$watch('thing', function (thing) {
coolService.doCoolStuffWith(thing);
}
Let the directive do the watching of its own scope, and put the shared functionality in the service.
This did it, and it allows me to set other members of the scope from within the watch:
angular.module('blah', []);
angular.module('blah').factory('BlahService', function() {
//constructor
function BlahService(scope) {
this._scope = scope;
this.myFunc = function() {
this._scope.otherVar = this._scope.someVar;
};
this._scope.$watch('someVar', function(newValue, oldValue, scope) {
// do cool stuff with thing
scope.otherVar = Number(scope.someVar) + 1;
return newValue;
});
}
return BlahService;
});
angular.module('blah').controller('BlahCtrl', function($scope, BlahService) {
$scope.someVar = 4;
$scope.BlahService = new BlahService($scope);
});
angular.module('blah').controller('Blah2Ctrl', function($scope, BlahService) {
$scope.someVar = 6;
$scope.BlahService = new BlahService($scope);
});
<html ng-app="blah">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body>
<div ng-controller="BlahCtrl">
1a. <input ng-model="someVar">
1b. <input ng-model="otherVar">
</div>
<div ng-controller="Blah2Ctrl">
2. <input ng-model="someVar">
2b. <input ng-model="otherVar">
</div>
</body>
</html>

why doesn't communication between two controllers using service work in angular?

index.html
<body >
<p ng-controller="MainCtrl as mv">Hello {{mv.name}}!</p>
<hr>
<div ng-controller="MainCtrl2 as mv">
<input type="text" ng-model="name">
<button ng-click="mv.setN(name)">submit</button><br>
Hello {{name}}<br>
Hello {{mv.name}}!
</div>
</body>
app.js :
var app = angular.module('plunker', []);
app.service('myService', function() {
var my = this;
my.name = "original";
});
app.controller('MainCtrl', function(myService) {
var mv = this;
mv.name = myService.name;
});
app.controller('MainCtrl2', function(myService) {
var mv = this;
mv.name = myService.name;
mv.setN = function(a) {
myService.name = a;
};
});
why isn't the service able to establish communication between controllers ?I have seen a similar example of factory which is working for communication.
my plunk:
http://plnkr.co/edit/L5uRHPQiXqQV6K7twHul?p=preview
Check the working demo: http://plnkr.co/edit/cTqJImTLdDwPzGsKxN6W?p=preview
Always use Object to share models. Your service should be changed to:
app.service('myService',function(){
var my=this;
my.n = {
name: 'paven'
};
my.setN=function(a){
my.n.name=a;
console.log("name change to "+my.n.name);
}
});

Using 'controller as' syntax to pass object values to parent controller

When using the $scope controller syntax, it's simple to set a value on a parent controller's object. For example
<div ng-controller="ParentController">
{{myValue.a}}
<div ng-controller="ChildController">
{{myValue.a}}
</div>
</div>
app.controller('ParentController', function($scope) {
$scope.myValue = {};
$scope.myValue.a = 1;
});
app.controller('ChildController', function($scope) {
$scope.myValue.a = 2;
});
The above outputs:
2
2
Is there a way to achieve the same functionality with the controller as syntax without referencing $scope in the child controller?
You could do it using a service, or you could do it referencing the scope.
The behavior that you are using, scope inheritance, is often referred to as an unwanted side affect. This is why isolated scopes are used with the controllerAs syntax.
In the following example you can see we achieve the same result using sharing the myValue property on the $scope along with the controllerAs syntax.
angular.module('app', [])
.controller('ParentController', ParentController)
.controller('ChildController', ChildController);
ParentController.$inject = ['$scope'];
function ParentController($scope) {
this.myValue = {};
this.myValue.a = 1;
$scope.myValue = this.myValue;
}
ChildController.$inject = ['$scope'];
function ChildController($scope) {
this.myValue = $scope.myValue;
this.myValue.a = 2;
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
<div ng-controller="ParentController as parent">
parent: {{parent.myValue.a}}
<div ng-controller="ChildController as child">
child: {{child.myValue.a}}
</div>
</div>
</div>
This can be accomplished without $scope using a service:
angular.module('app', [])
.controller('ParentController', ParentController)
.controller('ChildController', ChildController)
.service('valueService', ValueService);
ParentController.$inject = ['valueService'];
function ParentController(valueService) {
this.myValue = {};
this.myValue.a = 1;
valueService.setValue(this.myValue);
}
ChildController.$inject = ['valueService'];
function ChildController(valueService) {
this.myValue = valueService.getValue();
this.myValue.a = 2;
}
function ValueService() {
var storedValue;
this.getValue = function() {
return storedValue;
}
this.setValue = function(value) {
storedValue = value;
}
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
<div ng-controller="ParentController as parent">
parent: {{parent.myValue.a}}
<div ng-controller="ChildController as child">
child: {{child.myValue.a}}
</div>
</div>
</div>
if you don't like to use $scope you may pass outer controller downstream, see directives communication
No, it's not possible from within ChildController.
Don't think of ControllerAs as a newer style of $scope. Each has a different use.
ControllerAs does not publish values onto scope (it actually does - via the alias, but the alias should not be assumed to be known to a child Controller since the alias is defined in the View).
I use both where needed and I use the following convention:
app.controller("ParentCtrl", function($scope){
// $scope-inherited view model
var VM = $scope.VM = ($scope.VM || {});
// controller-specific view model
var vm = this;
VM.valueVisibleToChildControllers = "foo";
vm.valueVisibleOnlyToTheView = "bar";
});

How to return text to a div in angular js

I have a div in which i want to display a text, from a text-area. How to return the text from a text-area to a div in angularjs. I'm new to angularjs and don't know how it works. Please help.Thank you
<textarea data-ng-model="myModel"></textarea>
<div>{{ myModel }}</div>
I really suggest watching some videos as this is a very basic concept for angularjs
As #blaster said, a good way to share data between controllers is to use an Angular service.
A working example can be seen in this fiddle: http://jsfiddle.net/orlenko/5WhKW/
In this example, we define two controllers:
<div ng-controller="SourceController">
<textarea ng-model="message" ng-change="propagate()"></textarea>
</div>
<div ng-controller="DestinationController">
<div>{{message}}</div>
</div>
SourceController will be sending notifications about the data changes to DestinationController through a service.
The service uses $rootScope.$broadcast to let the world know it has an update:
myModule.factory('MessageSharing', function ($rootScope, $log) {
var share = {};
share.message = '';
share.broadcast = function (msg) {
$log.log('broadcasting ' + msg);
this.message = msg;
this.broadcastItem();
};
share.broadcastItem = function () {
$log.log('broadcasting this ' + this.message);
$rootScope.$broadcast('handleBroadcast');
};
return share;
});
Our destination controller will subscribe to the "handleBroadcast" event using $on:
function DestinationController($scope, $log, MessageSharing) {
$log.log('Initializing DestinationController');
$scope.message = '';
$scope.$on('handleBroadcast', function () {
$log.log('Got the message: ' + MessageSharing.message);
$scope.message = MessageSharing.message;
});
}
And finally, the SourceController will publish the updates through the service:
function SourceController($scope, $log, MessageSharing) {
$log.log('Initializing SourceController');
$scope.message = '';
$scope.propagate = function () {
$log.log('Propagating ' + $scope.message);
MessageSharing.broadcast($scope.message);
};
}

Resources