angularjs-controller

AngularJS Service returns undefined

泪湿孤枕 提交于 2019-12-01 09:45:07
I have the following service: app.services.emailService = ['$http', '$sce', function ($http, $sce) { return { getMessage: function(messageId, callback) { $http.get('/api/email/inbox' + '/' + messageId).then(function(response) { response.data.message.updated_at = new Date(response.data.message.updated_at.replace(/-/g,"/")); response.data.message.body = $sce.trustAsHtml(response.data.message.body); return response.data; }); } }; }]; In my controller I am assigning the return value to a $scope.message var so that I can display in the front end. $scope.message is undefined $scope.getMessage =

AngularJS Service returns undefined

心不动则不痛 提交于 2019-12-01 07:37:43
问题 I have the following service: app.services.emailService = ['$http', '$sce', function ($http, $sce) { return { getMessage: function(messageId, callback) { $http.get('/api/email/inbox' + '/' + messageId).then(function(response) { response.data.message.updated_at = new Date(response.data.message.updated_at.replace(/-/g,"/")); response.data.message.body = $sce.trustAsHtml(response.data.message.body); return response.data; }); } }; }]; In my controller I am assigning the return value to a $scope

Angular ngController vs Controller constructed within Directive

泄露秘密 提交于 2019-11-30 18:56:54
I'm wondering what the use cases are for these two methods of creating a controller: Using ngController: myApp.controller('myController', ['$scope', function ( $scope ) { }]); Constructing the controller within a directive with the controller attribute: myApp.directive ( 'myDirective', [ '$window', function( $window ) { return { restrict: 'A', controller: [ '$scope', function( $scope ) { }], link: function( scope, element, attrs ) { } }; }]); Is there any reason you wouldn't construct the controller within a directive if they were both invoked on the same element? Is it simply a question of

Call controller function from service in angularjs

无人久伴 提交于 2019-11-30 08:51:52
I am using socket.io to enable chat in my app and i am using a service SocketService to perform all the socket stuff. When a message came then i want to trigger a function of a controller from the service SocketService to make some changes in the UI. So i want to know that how can i access the function of a controller from the service. Sample Code: .service('SocketService', function ($http,$rootScope,$q) { this.connect = function(){ var socket = io(); socket.on('connect',function(){ // Call a function named 'someFunction' in controller 'ChatController' }); } }); This is the sample code for

AngularJS : How to create a two-way data binding between two isolated controllers and a shared service?

你离开我真会死。 提交于 2019-11-30 08:50:51
I am trying to create a two-way data binding between two isolated controllers and a shared service (which provides another isolated scope): app.factory("sharedScope", function($rootScope) { var scope = $rootScope.$new(true); scope.data = "init text from factory"; return scope; }); app.controller("first", function($scope, sharedScope) { $scope.data1 = sharedScope.data; }); app.controller("second", function($scope, sharedScope) { $scope.data2 = sharedScope.data; }); Fiddle: http://jsfiddle.net/akashivskyy/MLuJA/ When the application launches, data1 and data2 are correctly updated to the init

AngularJS passing data to bootstrap modal

一个人想着一个人 提交于 2019-11-30 06:23:18
问题 I think I'm missing something but cannot figure what. Basically I'm trying to pass an object to the modal like below, but instead of getting the passed object I gets null...so I think is a problem with the scope but I'm new in Angular and need some help. Controller app.controller("musicViewModel", function ($scope, $http, $location, $uibModal, $log) { $scope.selected = null; $scope.open = function (item) { $scope.selected = item; $log.info('Open' + $scope.selected); // get right passes object

Angular ngController vs Controller constructed within Directive

无人久伴 提交于 2019-11-30 03:17:50
问题 I'm wondering what the use cases are for these two methods of creating a controller: Using ngController: myApp.controller('myController', ['$scope', function ( $scope ) { }]); Constructing the controller within a directive with the controller attribute: myApp.directive ( 'myDirective', [ '$window', function( $window ) { return { restrict: 'A', controller: [ '$scope', function( $scope ) { }], link: function( scope, element, attrs ) { } }; }]); Is there any reason you wouldn't construct the

prevent default on submit :- Angularjs

南楼画角 提交于 2019-11-30 02:59:12
问题 I want to prevent-default action of http-post to '/signUp' if e-mail is null upon filling form. Controller Code:- $scope.signUp = function() { if($scope.email = null); preventdefault; } html (jade) :- form(name="input", action="/signUp", method="post") input(type="submit", value="submit") 回答1: When you have the action attribute specified for the form, angularjs will not do preventDefault. If you remove it and add ng-submit instead: <form name="myForm" method="post" ng-submit="signUp(myForm)"

Angular.js best practice - extending controllers, overriding controller defaults

限于喜欢 提交于 2019-11-29 20:12:10
Here is a real-life Angular problem I can't wrap my head around. I love Angular, but this issue is bugging me a lot right now. What is the best practice to extend an existing controllers's functions, and use the extended controller on an other page of my application? In other words: How to do controller inheritance in Angular? Edited out - 23/09/2014, dont think the description of my original usecase helps the visitors to understand it better what I'm after here. I think it disctracts people from the real issue. Mihaly KR After half a year I think I understand completely what's going on. As it

AngularJS: code not working when iterating through object [duplicate]

冷暖自知 提交于 2019-11-29 17:15:28
This question already has an answer here: 'this' vs $scope in AngularJS controllers 7 answers I am trying to populate a list of employee objects from my controller empctrl in a template. Here's the controller: app.controller('employeeController', function ($scope, employeeService) { this.employees = {}; this.populateTable = function (data) { this.employees = data; }; var error = function (err) { console.log("Error: " + err); }; // Call Service to List all Employees console.log("Service called to populate table."); employeeService.output().then(this.populateTable, error); this.populateTable();