I have am Angular Factory that creates global on screen alerts, but unsetting the alerts errors with an
"TypeError: undefined is not a function" error.
My Factory is:
'use strict';
angular.module('meanstackStarterApp')
.factory('Alerts', function ($timeout) {
var alertType = null;
var alertTitle = null;
var alertMessage = null;
return {
setMessage: function(type, title, message, timed) {
console.log("in");
this.alertType = type;
this.alertTitle = title;
this.alertMessage = message;
if (type == "alert-danger")
{
alertMessage = message + alertMessages.alertMessage_GlobalTryAgain;
}
if (timed)
{
// Set Timeout on unset
$timeout(this.unsetMessage(),5000);
}
},
unsetMessage: function() {
console.log("out");
this.alertType = null;
this.alertTitle = null;
this.alertMessage = null;
}
};
});
The setMessage function works fine but the unsetMessage errors on the this.
If I remove this prefix from the variables, the code runs through without erroring, but does not actually set the factory variables.
What am I missing?
I have here something like you, I'm changing your code to look like mine:
angular.module('meanstackStarterApp')
.factory('Alerts', function ($timeout) {
var self = this;
var alertType = null;
var alertTitle = null;
var alertMessage = null;
self.setMessage = function(type, title, message, timed) {
console.log("in");
self.alertType = type;
self.alertTitle = title;
self.alertMessage = message;
if (type == "alert-danger")
{
alertMessage = message + alertMessages.alertMessage_GlobalTryAgain;
}
if (timed)
{
// Set Timeout on unset
$timeout(self.unsetMessage(this),5000);
}
};
self.unsetMessage = function() {
console.log("out");
self.alertType = null;
self.alertTitle = null;
self.alertMessage = null;
};
return self;
});
Related
I get the following error:
TypeError: this.isUserLoggedIn is not a function
at userHasPermissionForView (accountService.js:159)
at Object.checkPermissionForView (accountService.js:155)
at edCareApp.js:232
When I call the function this.isUserLoggedIn in the following Service.
See below:
** Line 159: ** if (!this.isUserLoggedIn()) {
in the file accountService.js:
angular.module("edCare")
.service('accountService', function ($http, createAccountUrl, createAccountUserUrl, tokenUrl, tokenKey, $cookieStore, $rootScope) {
this.isUserAuthenticated = function () {
var token = sessionStorage.getItem(tokenKey);
if (token)
return true;
else
return false;
}
this.isUserLoggedIn = function () {
if ($rootScope.repository.loggedUser != null)
return true;
else
return false;
}
this.checkPermissionForView = function (view) {
return userHasPermissionForView(view);
};
var userHasPermissionForView = function (view) {
** Line 159: ** if (!this.isUserLoggedIn()) {
return false;
}
if (!view.permissions || !view.permissions.length) {
return true;
}
return userHasPermission(view.permissions);
};
this.userHasPermission = function (requiredPermissions) {
if (!this.isUserLoggedIn()) {
return false;
}
var found = false;
angular.forEach(requiredPermissions, function (requiredPermission, index) {
if ($rootScope.permissions.loggedUser.permissions.indexOf(requiredPermission)
>= 0) {
found = true;
return;
}
});
return found;
};
});
It looks a simple mistake, but I just can't see what I'm doing wrong.
Would someone please help.
Thanks.
The userHasPermissionForView function needs to be bound to the this context:
this.checkPermissionForView = function (view) {
̶r̶e̶t̶u̶r̶n̶ ̶u̶s̶e̶r̶H̶a̶s̶P̶e̶r̶m̶i̶s̶s̶i̶o̶n̶F̶o̶r̶V̶i̶e̶w̶(̶v̶i̶e̶w̶)̶;̶
return userHasPermissionForView.bind(this)(view);
};
var userHasPermissionForView = function (view) {
if (!this.isUserLoggedIn()) {
return false;
}
if (!view.permissions || !view.permissions.length) {
return true;
}
return userHasPermission(view.permissions);
};
I have next code
var D3s = new Array(9);
var D3n: float[];
result = Mine(result);
function Mine(block) {
D3n = new float[5];
var init = false;
if (block.startsWith("##new#")) {
block = block.replace("##new#", "");
init = true;
D3n[3] = 16;
}
var s1 = block.split("&");
s1.forEach(foreachBlocks);
if (init) {
//set default values
D3n[1] = 0;
D3s[6] = "ff";
D3n[2] = 0;
}
return TurnM79();
}
and error: CompilationErrorsException: script(9,9): BCE0005: Boo.Lang.Compiler.CompilerError: Unknown identifier: 'D3n'.
what wrong?
I have private values on my Factory service, but when I update a private value in one instance, all instances are updated as well.
Whats the best way to accomplish that without Updating all instances:
angular.module('app').factory('instanceItem', function () {
var _valid = true;
var item = function (id,data){
this.id = id;
this.data = data;
};
item.prototype.setValid = function(bool) {
_valid = bool;
}
item.prototype.getValid = function() {
return _valid
}
return { item:item }
});
What I am getting:
var itemA = new instanceItem.item(1,{});
var itemB = new instanceItem.item(2,{});
itemA.setValid(false);
console.log(itemA.getValid());
//False
console.log(itemB.getValid());
//False
What I am looking for
var itemA = new instanceItem.item(1,{});
var itemB = new instanceItem.item(2,{});
itemA.setValid(false);
console.log(itemA.getValid());
//False
console.log(itemB.getValid());
//True
A service is a singleton. So there is a unique instance of _valid. _valid should be a field of item:
var item = function (id,data){
this.id = id;
this.data = data;
this._valid = true;
};
item.prototype.setValid = function(bool) {
this._valid = bool;
}
item.prototype.getValid = function() {
return this._valid;
}
Or, if you really want to make it private (and also make your service more natural to use):
function createItem(id, data) {
var _valid = true;
return {
id: id,
data: data,
setValid: function(bool) {
_valid = bool;
},
getValid: function() {
return _valid;
}
};
}
return { createItem: createItem }
public Models.ManagedUsersPart GetManagedUsers(int ManagedUsersId)
{
return _cacheManager.Get(ManagedUsersId, ctx =>
{
MonitorManagedUserSignal(ctx, ManagedUsersId);
Timer = new Timer(t => DoUpdate(_contentManager,ManagedUsersId), "c", TimeSpan.FromMinutes(2), TimeSpan.FromMilliseconds(-1));
var managedusers = _contentManager.Get<ManagedUsersPart>(ManagedUsersId);
return managedusers;
});
}
and this is my DoUpdate function:
public void DoUpdate(IContentManager contentmanager,int ManagedUsersId)
{
var transation = _iworkcontext.CreateWorkContextScope().Resolve<ITransactionManager>();
transation.RequireNew();
var manager = getmanager();
var modifiemanageruser = manager.Get<ManagedUsersPart>(ManagedUsersId);
var modi = GetManagedUsers(ManagedUsersId);
modifiemanageruser.InvitedCount = modi.InvitedCount;
}
and ,this is my getmanager function:
public IContentManager getmanager()
{
if (Timermanager == null)
{
Timermanager = _iworkcontext.CreateWorkContextScope().Resolve<IContentManager>();
}
return Timermanager;
}
The question is "modifiemanageruser.InvitedCount = modi.InvitedCount"
this code does not persist update to database,anyone can help?
I create service like this :
export class MDCurrencyService implements IMDCurrencyService {
httpService: ng.IHttpService;
handlerUrl: string;
constructor($http: ng.IHttpService) {
this.httpService = $http;
this.handlerUrl = '/Master/';
}
get(): MDCurrency[]{
var result: MDCurrency[] = [];
var resp: ng.IPromise<any> = this.httpService.get(this.handlerUrl +'GetAllCurrency')
.then((response: any): ng.IPromise<any> => this.handlerResponded(response, null));
resp.then((data) => {
if (data.is_success) {
data.data.forEach(c => {
var converted: MDCurrency = <MDCurrency>c;
converted.selectedCountry = null;
converted.selectedStatus = null;
result.push(converted);
});
return result;
}
else return null;
});
return result;
}
handlerResponded(response: any, params: any): any {
response.data.requestParams = params;
return response.data;
}
}
in my controller :
$scope.currencies = this.currencies = mdCurrencyService.get();
if ($scope.currencies.length > 0) {
console.log('entered'); //never executed
$scope.currencies.forEach(currency => {
for (var ii = 0; ii < this.statuses.length; ii++)
if ($scope.statuses[ii].Id == currency.Status)
currency.selectedStatus = this.statuses[ii];
});
}
but after $scope.currencies filled from service that forEach never executed.
how to execute that code when $scope.currencies is filled by data from service?
mdCurrencyService.get() should be implemented as an asynchronous service returning a promise. It can be utilized this way:
mdCurrencyService.get().then(function (currencies) {
// process result here
$scope.currencies = currencies;
});