angularjs-service

What is the difference between `value` attribute and `ng-value` attributes in angularjs

三世轮回 提交于 2019-11-28 07:33:50
What is the difference between value and ng-value attributes in angularjs templates? If I use ng-if on the field using value attribute it works properly but if I change the attribute value to ng-value it stops working. example 1 // it works <input type='radio' ng-model='difficulty' value='hard'/> <div ng-if="difficulty == 'hard'"> <p>difficulty is hard</p> </div> Example 2 // it doesn't work <input type='radio' ng-model='level' ng-value='hard'/> <div ng-if= "level == 'hard'" > <p>level is hard</p> </div> According to the docs , ngValue takes an "angular expression, whose value will be bound to

Can someone provide a use case for the $controller service in AngularJS?

喜你入骨 提交于 2019-11-28 06:59:05
Angularjs docs give the usage of $controller service as: $controller(constructor, locals); Can anyone focus some light on these 2 points: When to use $controller service. Please provide some use case. Details about 'locals' parameter passed to it. You can create common functions which are to be executed on $scope into one controller may be named 'CommonCtrl' . angular.module('app',[]).controller('CommonCtrl', ['$scope', function($scope){ var self = this; $scope.stuff1 = function(){ } $scope.stuff2 = function(){ } self.doCommonStuff = function(){ // common stuff here $scope.stuff1(); $scope

Injecting a service into another service in angularJS

我只是一个虾纸丫 提交于 2019-11-28 03:38:10
Is it possible to inject one service into another service in angularJS? Yes. follow the regular injection rule in angularjs. app.service('service1', function(){}); //Inject service1 into service2 app.service('service2',function(service1){}); Thanks to @simon. It is better to use Array injection to avoid minifying problem. app.service('service2',['service1', function(service1) {}]); Yes. Like this (this is a provider, but same thing applies) module.provider('SomeService', function () { this.$get = ['$q','$db','$rootScope', '$timeout', function($q,$db,$rootScope, $timeout) { return reval; } });

Angular Service Definition: service or factory

雨燕双飞 提交于 2019-11-28 01:16:01
I am an angular newbie, I am building an application, one thing really puzzling me is there are couple of ways of defining a service, and I read more from this link: How to define service then it seems there is no big difference among the ways of defining a service. but I just noticed one difference which I think is different: see this service I get from here http://jsfiddle.net/2by3X/5/ var app = angular.module('myApp', []); app.service('test', function($timeout, $q) { var self = this; this.getSomething = function() { return self.getData().then(function(data) { return self.compactData(data);

Simple Angular $routeProvider resolve test. What is wrong with this code?

醉酒当歌 提交于 2019-11-28 01:11:38
问题 I have created a simple Angular JS $routeProvider resolve test application. It gives the following error: Error: Unknown provider: dataProvider <- data I would appreciate it if someone could identify where I have gone wrong. index.html <!DOCTYPE html> <html ng-app="ResolveTest"> <head> <title>Resolve Test</title> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"> </script> <script src="ResolveTest.js"></script> </head> <body ng-controller="ResolveCtrl"> <div ng

how to reuse a function in multiple controllers

ⅰ亾dé卋堺 提交于 2019-11-28 00:37:26
问题 I have multiple controllers for multiple routes: app.controller('FirstController', function ($scope) { $scope.func = function () { console.log('route 1'); } } app.controller('SecondController', function ($scope) { $scope.func = function () { console.log('route 2'); } } ... and a directive that uses the $scope.func , this way: app.directive('thedirective', function () { return { link: function (scope, $element, attrs) { $scope.func(attrs.thedirective); } } }); $scope.func is different in each

Should services expose their asynchronicity?

半世苍凉 提交于 2019-11-27 20:10:04
I'm writing a service that will retrieve data asynchronously ($http or $resource). I can hide the fact that it is asynchronous by returning an array that will initially be empty, but that will eventually get populated: .factory('NewsfeedService1', ['$http', function($http) { var posts = []; var server_queried = false; return { posts: function() { if(!server_queried) { $http.get('json1.txt').success( function(data) { server_queried = true; angular.copy(data, posts); }); } return posts; } }; }]) .controller('Ctrl1', ['$scope','NewsfeedService1', function($scope, NewsfeedService1) { $scope.posts

Call angularjs service from simple js code

孤者浪人 提交于 2019-11-27 18:32:19
I have the following angularjs service: angular.module('app.main').factory('MyService', ["$http", function ($http) { return new function () { this.GetName = function () { return "MyName"; }; }; }]); How can I call GetName function from MyService from legacy js code? mitch Use angular.injector . Using your code you can do something like the following: angular.module('main.app', []).factory('MyService', ['$http', function ($http) { return new function () { this.GetName = function () { return "MyName"; }; }; }]); angular.injector(['ng', 'main.app']).get("MyService").GetName(); Here is the

How to close Angular UI Modal from anywhere

泪湿孤枕 提交于 2019-11-27 18:29:14
I am using the Angular UI bootstrap modal dialog and create it within a service: myApp.factory('ModalService', ['$modal', function($modal) { 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() { // this should close all modal instances } }; }]); How can I close all modal instances when calling ModalService.close() from a controller or whatsoever?

Force AngularJS service to return data before loading controller

我与影子孤独终老i 提交于 2019-11-27 18:21:37
I have a service in Angular which uses my API to get user information and provides it to my controllers. It's set up like this: angular.module('myApp', ['myApp.filters', 'myApp.services', 'myApp.directives', 'ngResource', 'infinite-scroll', 'ui.bootstrap', 'ngCookies', 'seo']) .service('userInfo', function($http, $cookies){ $http.get('/api/users/' + $cookies.id). success(function(data) { var userInfo = data.user[0]; return userInfo; }); }). // other stuff comes after this In my controllers, I include it like: function userProfile($scope, $cookies, userInfo, $http, $resource, $routeParams,