angularjs-controller

AngularJS common controller functionality - mix-in where needed or define on $rootScope?

守給你的承諾、 提交于 2020-01-03 10:39:26
问题 I am using v1.2.0 rc2 of AngularJS and want to know what is the best method to provide common functionality to multiple controllers. I have the following validation functions that I want to use in all controllers that edit a model: $scope.canSave = function (formController) { return formController.$dirty && formController.$valid; }; $scope.validationClasses = function (modelController) { return { 'has-error': modelController.$invalid && modelController.$dirty, 'has-success': modelController.

How to share the $scope variable of one controller with another in AngularJS?

北战南征 提交于 2019-12-28 05:48:08
问题 I have this: app.controller('foo1', function ($scope) { $scope.bar = 'foo'; }); app.controller('foo2', function ($scope) { // want to access the $scope of foo1 here, to access bar }); How would I accomplish this? 回答1: You could use an Angular Service to share variable acrosss multiple controllers. angular.module('myApp', []) .service('User', function () { return {}; }) To share the data among independent controllers, Services can be used. Create a service with the data model that needs to be

Extending base directive functionality to other directives

匆匆过客 提交于 2019-12-25 14:06:06
问题 I just wanted to check is this functionality is possible in angularjs. Eg: I have base directive with some common functions but in my whole application all the directives need this functions to be implemented so more code duplication occurs, I am expecting same functionality as extends(inheritance) in Java. Is it possible to achieve this functionality in angularjs.? As per my requirement i can't archive this function by moving this code to services or factory because it is like all of by

Unit-Testing a service in Controller with Jasmine in AngularJS

北城以北 提交于 2019-12-24 11:25:17
问题 In my Controller I've defined the following service: CrudService.getAllGroups().$promise.then( function (response) { $scope.groups = response; }, function (error) { //error code.. } ); Well, I want to test this service whether it gets a response or not. In test script at first I've defined a function to check whether the service is defined at all. Test code: describe('Ctrl: TestCtrl', function () { beforeEach(module('testApp')); var scope, CrudService, ctrl, backend; beforeEach(inject

Angular: Not able to access variables in controller using service

亡梦爱人 提交于 2019-12-24 01:20:10
问题 I am trying to share a variable between a controller and a function. But i get an error from the controller, saying this: TypeError: Cannot read property 'getSet' of undefined I have gone through numerous tutorials, but don't know where am I going wrong. My service code is like this: app.service('shareData', function() { var selected = ["plz", "print", "something"]; var putSet = function(set) { selected = set; }; var getSet = function() { return selected; }; return { putSet: putSet, getSet:

how to reference a controller inside a sub-module in angularjs

[亡魂溺海] 提交于 2019-12-24 00:07:20
问题 I'm using modules /sub modules on the angular app, my controller doesn't load on a specific route but the view does, according to a comment on this question I should reference the child module inside the main module and that should do the trick. this is my code for bootstrapping the app: angular.module('mainApp', ['ui.bootstrap', 'ui.utils', 'ui.router', 'ngResource', 'ngAnimate', 'ngCookies', 'facebook', 'subModule1', 'subModule2', 'subModule3']); angular.module('mainApp').config(function (

How to pass ngModel to AngularJS component without 2-way binding?

穿精又带淫゛_ 提交于 2019-12-23 03:58:07
问题 I have custom input as AngularJS 1.5 component with one-way data bindings and ngModel as two-way data binding: return { bindings: { form: "<", fieldName: "@", minLength: "@", maxLength: "@", isRequired: "@", errors: "<", value: "=ngModel" }, templateUrl: "actTextField.html", controller: ActTextFieldController, } in file actTextField.js in provided Plunk. I want to follow A new Angular 1.x ES2015 styleguide, the path to Angular 2 where Todd wrote: We no longer should utilise two-way data

AngularJS - Is there a way to inject data into a controller without using routes?

北慕城南 提交于 2019-12-23 03:11:40
问题 I have a controller in my angular application that requires some server-side initialization. I would like to do it synchronously, meaning that the data should be fetched before the controller is initiated. I synchronously load and inject data into many other controllers using the routeProvider in application configuration: $routeProvider .when('/Home', { templateUrl: 'default/home', controller: 'homeController', resolve: { 'InitPageData': ['initPageService', function (initPageService) {

AngularJS load remote JS controller provided by webservice

杀马特。学长 韩版系。学妹 提交于 2019-12-22 13:58:17
问题 I have a AngularJS app, that depends on a webservice, I would like to load some more controllers into the app from a remote host, after the app is loaded. I don't know i this is possible? In my controller, I want to load some more controllers (js files) .controller('FrontpageCtrl', function($scope, $stateParams, $filter, $sce, contentService) { console.log("hitting FrontpageCtrl ... "); contentService.promise.then(function(data){ var page = $filter('filter')(data, {id:$stateParams.pageId})[0]

AngularJS: Cannot interpolate attribute from first directive to a second. (w/ plunker example)

谁说胖子不能爱 提交于 2019-12-22 12:28:40
问题 Reference Reference plunker: http://plnkr.co/edit/otv5mVVQ36iPi3Mp0FYw?p=preview Explanation of the issue Suppose that we have two directives, first-directive and second-directive . Now suppose we only have access to first-directive which we hope to wrap second-directive with and pass to it our own manipulated attributes. app.directive('firstDirective', function() { return { scope: true, priority: 1000, transclude: true, template: function(element,attributes){ console.log('template') return '