$filter is not a function AngularJS - javascript

app.controller('myController', ['$scope', '$http', '$filter', function($scope, $http, $filter) {
The above is an example of my code where I am trying to use $http.get and also $filter inside my controller.
The only problem is when I use it like so, the console log throws an error that says $filter is not a function.
app.controller('myController', ['$scope', '$http', '$filter', function($scope, $filter, $http) {
When I swap them round it throws an error that $http is undefined

When you are using
app.controller('myController', ['$scope', '$http', '$filter', function($scope, $filter, $http) {
variable $filter is actually a instance of $http, and $http is instance of $filter. Actually it doesn't matter what is written in function(...) params.
What is important here, is the order of injectibles you are using, for example
app.controller('myController', ['$scope', '$http', '$filter', function(a, b, c) {
will map to instances:
a -> scope
b -> $http
c -> $filter
From angular docs:
Since Angular infers the controller's dependencies from the names of arguments to the controller's constructor function, if you were to minify the JavaScript code for PhoneListCtrl controller, all of its function arguments would be minified as well, and the dependency injector would not be able to identify services correctly.
So by using array notation for yout controller, you are making sure that code will work after code is minified.

add filter after http and angular version also defends how you are
going to use filter.
plateFormController.$inject = ['$scope', '$http',
'$filter','$timeout', '$q', '$mdSidenav', '$log'];
function plateFormController($scope, $http,$filter, $timeout, $q) {
jsonByName=$filter('filter')($scope.json, { name: 'a' });
}

In my case, expected data List object is null, by ensuring that expected List Object not null. I am able to avoid this error.

Related

Resharper rule "Function Parameter" does not accept angularjs service name

In my angularjs app all my service names start with an uppercase character. I would like to be able the allow service parameters to match the the service name, however the JavaScript "Function Parameter" name rule in Resharper does not allow parameters that start with an uppercase character.
Is it possible to configure or change the JavaScript "Function Parameter" name rule in Resharper to allow service names that start with an uppercase character?Or is there some other way to to avoid this warning?
The BudgetingService parameter in the following code is flagged as a warning by Resharper with the message: "Name 'BudgetingService' does not match rule 'Function Parameter'. Suggested name is 'budgetingService'."
app.controller('BudgetingController',
['$scope', '$rootScope', '$window', 'BudgetingService',
function ($scope, $rootScope, $window, BudgetingService) {
// ...
}]);
Not the answer you are looking for, but there is a reason for that. the only time variables in javascript should be ConstantCamelCase is when they are classes/constructors.
if anything the actual service you create should be budgetingService, not BudgetingService
just lowercase the name it's not work the fight you're trying to put up
app.controller('BudgetingController',
['$scope', '$rootScope', '$window', 'BudgetingService',
function ($scope, $rootScope, $window, budgetingService) {
budgetingService.whatever()
// ...
}
]);
if you REALLY want to have it uppercase, redeclare it after injecting
app.controller('BudgetingController',
['$scope', '$rootScope', '$window', 'BudgetingService',
function ($scope, $rootScope, $window, budgetingService) {
var BudgetingService = budgetingService;
// ...
}
]);
Resharper provides a documentation to change default namings
Here is documentation https://www.jetbrains.com/help/resharper/2016.3/Coding_Assistance__Naming_Style.html

AngularJS: loading async translations issue

I am building a Website with AngularJS. I ran into problems with translations, got a hacky more or less working solution to it but i am thinking that it can be done a lot better.
I have a main controller which loads translations from database.
$http.get($rootScope.ApiUrl + '/?a=sprache&lang=' + $rootScope.lang).success(function (data) {
$scope.spr = data;
$rootScope.translations = data;
$rootScope.updateTranslations();
});
data is an array formatted like this:
{key: "translation",…}
Further on I have a controller for each state. I hoped it could do something like this:
app.controller('InventoryCtrl', [
'$scope',
'$http',
'$location',
'$state',
'$stateParams',
'$rootScope',
'$uibModalStack',
function ($scope, $http, $location, $state, $stateParams, $rootScope, $uibModalStack) {
$scope.title = $rootscope.translations.myTranslatedTitleForThisState
]);
Obviously this does not work as the get-request is not finished before this Code gets called and therefore $rootscope.translations variable is not set.
Instead I wrote the following. The updateTranslations() function is called from the loadTransition() function in MainController (above) after successfully finishing the get-request.
app.controller('InventoryCtrl', [
'$scope',
'$http',
'$location',
'$state',
'$stateParams',
'$rootScope',
'$uibModalStack',
function ($scope, $http, $location, $state, $stateParams, $rootScope, $uibModalStack) {
$rootScope.updateTranslations = function() {
$rootScope.setMetaTags($rootScope.translations.inventory_title, $rootScope.translations.inventory_description);
$rootScope.updateTranslations();
}
}
]);
I am pretty sure this can be done a lot better. Any ideas?
Maybe this answer is not a direct answer to your problem, but a possible new approach...
We have been using ui-router quite alot in recent Angular projects.
ui-router is ideal for defining routes within the application. More about that subject here.
A nice feature is the state resolvement. Meaning that the state will only resolve when the resolvement promise is resolved.
By example
angular.module('testApp').config(function($stateProvider) {
$stateProvider.state('', {
url: '/',
templateUrl: 'main.html',
resolve: {
labels: function(labelService) {
return labelService.loadLabels();
}
}
});
});
In the above example, the state / will resolve after the $http.get promise behind the loadService.loadLabels() is resolved. Meaning the template will be loaded, after all the resolvements are resolved.
In this case, your view will be loaded - after that all your labels are loaded and accessible by the controller (later the view).
A nice thing is that nested state definitions are perfectly possible.
Meaning, that you could have 1 root state, and many child state of the root state.

Using Cookie value from one ng-app in another ng-app in AngularJS

I know that it isn't good practice to have multiple ng-apps in a project but my project is divided into segments that aren't generally related to each other, plus I am left with more or less no other option.
My problem is the persistence of ng-cookie amidst two separate Javascript files. Let's say I have dashboard1.html that is linked to controller1.js and dashboard2.html linked to controller2.js.
Now, I want to access the cookie value of controller 1 in controller 2. When I try to do this the result says undefined and that means I cannot access the cookie value set by another Javascript file.
Please advise me how to solve this issue.
NOTE : Javascript files have respective ng-app defined and controller for each application.
This is Controller and ng-app part of one module.
var app = angular.module('mainpageapp', ['ngRoute','ngCookies']);
app.controller('ctrlmainpage', ['$scope', '$timeout','$http', '$route', '$routeParams', '$location', '$rootScope', '$cookies', function ($scope, $timeout, $http, $route, $routeParams, $location, $rootScope, $cookies) {
console.log("Csadad");
$cookies.myFavorite = 'TestCookie';
var favoriteCookie = $cookies.myFavorite;
console.log("Cookie" + favoriteCookie);
Now in this ng-app's controller I am setting a cookie, it works properly and I do get expected response on console. This is in file controller1.js
Now I am trying to access the same cookie in controller2.js that is all together bound by different ng-app.
var app = angular.module('myApp', ['ngRoute', 'smart-table', 'ngCookies']);
var id = 'hasdh';
app.controller('dashmerctrl', ['$scope', '$timeout','$http', '$route', '$routeParams', '$location', '$rootScope', '$cookies', function ($scope, $timeout, $http, $route, $routeParams, $location, $rootScope, $cookies)
{
var favoriteCookie1 = $cookies.myFavorite;
console.log("Cookie1" + favoriteCookie1);
Her I get undefined as output.
I want to access that same cookie in this file.
if you wish to share data across tabs or domains you can give zendesk's "cross-storage" a try, just implemented it with AngularJS and it works like a charm.
it utilizes window.postMessage() technique to achieve this behavior.
link: zendesk/cross-storage

Why do angularjs controller declaration have this syntax structure?

I see the following angularjs controller syntax structure all the time.
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location',
function ($scope, $interval, $location)
{
}]);
Why the repetition in the parameter names? Why not just like this
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location',
function ()
{
}]);
or
angular.module('7minWorkout').controller('WorkoutController',
[
function ($scope, $interval, $location)
{
}]);
The array syntax will help you with minification/uglify of your js code.
angular.module('7minWorkout').controller('WorkoutController',
function ($scope, $interval, $location) {
// code here
});
Will be minified and mangled as:
angular.module('7minWorkout').controller('WorkoutController',
function (a, b, c) {
// code here
});
So Angular won't be able to figure out which dependencies to inject
On the other hand, using the array declaration:
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location', function ($scope, $interval, $location) {
// code here
}]);
Will be minified as :
angular.module('7minWorkout').controller('WorkoutController',
['$scope', '$interval', '$location', function (a, b, c) {
// code here
}]);
So Angular will know what a, b and c represent.
There's also another way to inject variables if you use your first example code like below:
WorkoutController.$inject = ['$scope', '$interval', '$location'];
or
angular.module('7minWorkout').controller('WorkoutController', /* #ngInject */
function ($scope, $interval, $location) {
// code here
});
which will create the $inject method mentioned above when the code is annotated.
So, there are mainly four kinds of annotation:
Implicit Annotation - the first example code
Inline Array Annotation - the second example code
$inject Property Annotation - the $inject method
$ngInject Comment Annotation - the #ngInject method
ng-annotate
Tools like ng-annotate let you use implicit dependency annotations in your app and automatically add inline array annotations prior to minifying. If you decide to take this approach, you probably want to use ng-strict-di.
For more information, see AngularJS Developer Guide - Using Strict Dependency Injection.
This "repetion" is to make it safe for minification:
AngularJS - Controllers, Dependencies, and Minification
or you can use following syntax, according to popular angular-styleguide https://github.com/johnpapa/angular-styleguide
angular.module('7minWorkout')
.controller('WorkoutController', WorkoutController);
WorkoutController.$inject = ['$scope', '$interval', '$location'];
function WorkoutController($scope, $interval, $location) {
}
You could write the first version since it just omits the parameters of the function which are also accesible via arguments inside the function. So you would avoid the repition but slicing the arguments property is also not really efficient (compared to just type out the parameters).
As the others answers stated the repition is to make it safe for minification.
The first controller syntax makes it possible to minify/uglify the javascript code with the use of tools like ngmin. I'm not quite sure if the 2nd and 3rd options you have provided are viable options to create a controller, but in any case they will not be minified correctly since the tools will not now what the providers are. I would either suggest to use option 1 or option 3 (without the brackets) to create a controller. Note that option 3 will not be minified correctly by automated tools.
Some Useful information about creating controllers:
AngularJS Developer Guide - Controllers
option3 without brackets
angular.module('7minWorkout').controller('WorkoutController', function ($scope, $interval, $location)
{
//Your Code
});

Accessing $rootScope methods in Controller

In this ng-book JSBin, does $scope.$watch() resolves to $rootScope.$watch() due to prototypal inheritance.
Can we inject $rootScope explicitly inside the controller so that $scope would be same as $rootScope inside the controller, without going through prototypal inheritance?
Replicating code here for reference:
// open this example and type person.name into the test field
angular.module('myApp', [])
.controller('MyController',
['$scope', '$parse', function($scope, $parse) {
$scope.person = {
name: "Ari Lerner"
};
$scope.$watch('expr', function(newVal, oldVal, scope) {
if (newVal !== oldVal) {
// Let's set up our parseFun with the expression
var parseFun = $parse(newVal);
// Get the value of the parsed expression, set it on the scope for output
scope.parsedExpr = parseFun(scope);
}
});
}]);
Just inject it the same way as $scope or $parse, anything defined on $rootScope will be then accessible inside your controller.
app.controller('MyController', ['$scope', '$parse', '$rootScope',
function($scope, $parse, $rootScope) {
$rootScope.foo();
console.log($rootScope.bar);
}
]);
etc.
If you intend to use the rootScope so badly, it has a provider just as scope does. Including '$rootScope' into your controller as you do with the '$scope' does the trick.
There's also the $parent attribute of the $scope which could come in handy, but IMO it tends to make code less maintainable if abused. In particular when multiple scopes are nested, as you need to traverse the whole hierarchy.

Categories

Resources