AngularJS Service vs factory vs provider

Published on

Service vs factory vs provider. What are the differences between AngularJS  Service, Factory and Provider? A Good understanding of Service, Factory and Provider. From the AngularJS mailing list I got an amazing thread that explains service vs factory vs provider and their injection usage. Compiling the answers: Services Syntax: module.service( ‘serviceName’, function ); Result: When declaring serviceName as … Continue reading AngularJS Service vs factory vs provider

Best practice for Pass variables to AngularJS controller

Published on

You could use ng-init in an outer div: <div ng-init=”param=’value’;”> <div ng-controller=”BasketController” > <label>param: {{value}}</label> </div> </div> The parameter will then be available in your controller’s scope: function BasketController($scope) { console.log($scope.param);} ———————————————— OR ——————————————————- You could create a basket service. And generally in JS you use objects instead of lots of parameters. Here’s an example: … Continue reading Best practice for Pass variables to AngularJS controller

How to passing data between controllers in Angular JS?

Published on

From the description, seems as though you should be using a service. Check outhttp://egghead.io/lessons/angularjs-sharing-data-between-controllers and AngularJS Service Passing Data Between Controllers to see some examples. You could define your product service as such: app.service(‘productService’, function() { var productList = []; var addProduct = function(newObj) { productList.push(newObj); }; var getProducts = function(){ return productList; }; return … Continue reading How to passing data between controllers in Angular JS?

How to access controller $scope from outside in Angularjs?

Published on

Without seeing the markup, I guess the scope of MenuSideController is a child scope to the scope you are selecting. While it is possible to traverse down the tree like this (assuming the scope we want is the first child): var appElement = document.querySelector(‘[ng-app=theApp]’); var appScope = angular.element(appElement).scope(); var controllerScope = appScope.$$childHead; console.log(controllerScope.user); It is … Continue reading How to access controller $scope from outside in Angularjs?

How to access parent scope from child controller in Angularjs?

Published on

If your HTML is like below you could do something like this: <div ng-controller=”ParentCtrl”> <div ng-controller=”ChildCtrl”> </div> </div> Then you can access the parent scope as follows function ParentCtrl($scope) { $scope.cities = [“NY”, “Amsterdam”, “Barcelona”]; } function ChildCtrl($scope) { $scope.parentcities = $scope.$parent.cities; } If you want to access a parent controller from your view you … Continue reading How to access parent scope from child controller in Angularjs?

How to get controller name from $scope in angularjs

Published on

I’m not sure this is a good solution, but I was able to inject $scope.controllerName using this technique: app.config([‘$provide’, function ($provide) { $provide.decorator(‘$controller’, [ ‘$delegate’, function ($delegate) { return function(constructor, locals) { if (typeof constructor == “string”) { locals.$scope.controllerName = constructor; } return $delegate(constructor, locals); } }]); }]); Then app.controller(‘SampleCtrl’, [‘$scope’, ‘$log’, function ($scope, $log) … Continue reading How to get controller name from $scope in angularjs

Angular UI-Router get the current state from within a view

Published on

Couldn’t find this documented anywhere, so I looked in the source code. There is a data field named $uiView attached to the ui-view element, it contains the view name and the associated state. You can get this state like this: elem.closest(‘[ui-view]’).data(‘$uiView’).state or even elem.inheritedData(‘$uiView’).state