how to call service method from ng-change of select in angularjs?

You have at least two issues in your code: ng-change=”getScoreData(Score) Angular doesn’t see getScoreData method that refers to defined service getScoreData: function (Score, callback) We don’t need to use callback since GET returns promise. Use then instead. Here is a working example (I used random address only for simulation): HTML <select ng-model=”score” ng-change=”getScoreData(score)” ng-options=”score as … Read more

Force AngularJS service to return data before loading controller

You can make the factory to return a promise like this: angular.module(‘myApp’, [‘myApp.filters’, ‘myApp.services’, ‘myApp.directives’, ‘ngResource’, ‘infinite-scroll’, ‘ui.bootstrap’, ‘ngCookies’, ‘seo’]) .service(‘userInfo’, function ($http, $cookies) { var promise = $http.get(‘/api/users/’ + $cookies.id). success(function (data) { var userInfo = data.user[0]; return userInfo; }); return promise; }) // other stuff comes after this And in your controller, do … Read more

Delay an angular.js $http service

Interesting question! As you mentioned yourself, $timeout is the most logical choice for a delayed call. Instead of having $timeout calls everywhere, you could push a response interceptor that wraps the $http promise in a $timeout promise, as conceptually outlined in the documentation of $http, and register it in one of your configuration blocks. This … Read more

Inject dateFilter in a service in AngularJs

First you have to inject the filter like this: app.factory(‘myService’, [‘$rootScope’, ‘$filter’, function($rootScope, $filter) (The array is only needed when you use minification in your build process) To call specific filter programmatically: $filter(‘date’)(new Date(), ‘yyyy-MM-01’); $filter(name)returns the specific filter function, which you can than call with your arguments: var dateFilter = $filter(‘date’); var filteredDate = … Read more

How can I define an AngularJS service using a TypeScript class that doesn’t pollute the global scope?

2016-05-06: New example using ES6-style modules The static $inject array and constructor remain unchanged from the previous example. The only change is to split the classes into multiple files and use ES6 modules to pull in the class definitions. /lib/HelloService.ts: export class HelloService { public getWelcomeMessage():String { return “Hello from HelloService”; } } /lib/AnotherService.ts: import … Read more

Creating common controller functions

The way to define common code in angular is through Services. You would define a new service like so : .factory(‘CommonCode’, function ($window) { var root = {}; root.show = function(msg){ $window.alert(msg); }; return root; }); In your controller you would inject this service..like so function MainAppCtrl($scope,CommonCode) { $scope.alerter = CommonCode; $scope.alerter.show(“Hello World”); } Just … Read more

How to close Angular UI Modal from anywhere

Inject the $modalStack service and call the function $modalStack.dismissAll(), see the code on GitHub for details: myApp.factory(‘ModalService’, [‘$modal’, ‘$modalStack’ function($modal, $modalStack) { return { trigger: function(template) { $modal.open({ templateUrl: template, size: ‘lg’, controller: function($scope, $modalInstance) { $scope.ok = function() { $modalInstance.close($scope.selected.item); }; $scope.cancel = function() { $modalInstance.dismiss(‘cancel’); }; } }); }, close: function(reason) { $modalStack.dismissAll(reason); … Read more

Cordova + Angularjs + Device Ready

Manually bootstrap your Angular app: Remove your ng-app attribute from your HTML code, so Angular doesn’t start itself. Add something like this to you JavaScript code: document.addEventListener(“deviceready”, function() { // retrieve the DOM element that had the ng-app attribute var domElement = document.getElementById(…) / document.querySelector(…); angular.bootstrap(domElement, [“angularAppName”]); }, false); Angular documentation for bootstrapping apps.

$watch inside a service?

You can add any expression to the set of watches by injecting the $rootScope and using a function a first argument to the $watch method. Pseudo-code: $rootScope.$watch(function() { return //value to be watched; }, function watchCallback(newValue, oldValue) { //react on value change here }); Don’t forget about the third, Boolean argument to the $watch method. … Read more

tech