- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有这样的结构:
//function declaration
someFunc() = {
// ...
}
//assign function to variable
let f = someFunc();
//call
f();
我想通过变量调用此函数来将参数传递给 someFunc(),例如:
f(arg);
但是 f 不是一个变量。这样如何处理fucntion中的参数呢?
编辑:在上面的问题中,我想到了通过变量调用函数。像这样的东西:
{{ F }}并且 f 被分配给返回某个值的函数。
为什么我这样做是因为我需要在 ngOnInit 生命周期中初始化函数(我的目标是只调用函数一次)。
我的 View 和 ts 代码如下:
TS:
import {Component, OnInit, Input} from '@angular/core';
import {Router} from "@angular/router";
import {PostsService} from "../../services/posts.service";
import {AuthService} from "../../services/auth.service";
import {ConfirmationModalService} from "../../services/confirmation-modal.service";
import {LoaderService} from "../../services/loader.service";
import {ToastService} from "../../services/toast.service";
declare var M: any;
@Component({
selector: 'app-posts',
templateUrl: './posts.component.html',
styleUrls: ['./posts.component.scss']
})
export class PostsComponent implements OnInit {
avatar: string;
loggedUser: any;
modalId: any;
editModal: any;
editModalInstance: any;
content: string;
contentEdited: string;
orderSelect: any;
orderSelectInstance: any;
showButton: boolean;
getAvatar: any;
constructor(private postsService: PostsService, private authService: AuthService,
private router: Router, private confirmationModalService: ConfirmationModalService,
private loaderService: LoaderService, private toastService: ToastService) {
}
ngOnInit() {
this.showButton = true;
this.editModal = document.querySelector('#edit-modal');
this.editModalInstance = M.Modal.init(this.editModal);
this.orderSelect = document.querySelectorAll('#post-order-select');
this.orderSelectInstance = M.FormSelect.init(this.orderSelect);
this.orderSelect = 'post_date';
this.loggedUser = localStorage.getItem('user') ?
JSON.parse(localStorage.getItem('user')).id : '';
this.loaderService.isLoading = true;
this.postsService.getPosts().subscribe(response => {
this.postsService.getPostHandling(response);
this.loaderService.hideLoader();
}, err => {
this.toastService.error(err);
this.loaderService.hideLoader();
return false;
});
this.getAvatar = this.getCreatorAvatar(123);
}
loadMore() {
this.loaderService.isLoadingSmall = true;
if (this.orderSelect === 'post_date') {
this.postsService.getPosts(true).subscribe(response => {
if (this.postsService.postIndex > response.posts.length) {
this.showButton = false;
}
this.postsService.getPostHandling(response);
this.loaderService.hideLoaderSmall();
}, err => {
this.toastService.error(err);
this.loaderService.hideLoaderSmall();
return false;
});
} else if (this.orderSelect === 'rate') {
this.postsService.getPostsOrdered(true).subscribe(response => {
if (this.postsService.postIndex > response.posts.length) {
this.showButton = false;
}
this.postsService.getPostHandling(response);
this.loaderService.hideLoaderSmall();
}, err => {
this.toastService.error(err);
this.loaderService.hideLoaderSmall();
return false;
});
}
}
order(orderSelect) {
this.loaderService.isLoading = true;
if (orderSelect === 'post_date') {
this.postsService.getPosts().subscribe(response => {
this.postsService.getPostHandling(response);
}, err => {
this.toastService.error(err);
return false;
});
} else {
this.loaderService.isLoading = true;
this.postsService.getPostsOrdered().subscribe(response => {
this.postsService.getPostHandling(response);
}, err => {
this.toastService.error(err);
return false;
});
}
this.loaderService.hideLoader();
}
onEditModalSubmit() {
this.editModalInstance.close();
this.postsService.editPost(this.modalId, this.contentEdited).subscribe(data => {
if (data.success) {
this.loaderService.isLoading = true;
this.postsService.getPosts().subscribe(response => {
this.postsService.getPostHandling(response);
}, err => {
this.toastService.error(err);
return false;
});
this.loaderService.hideLoader();
this.toastService.success(data.msg);
} else {
console.log('some error');
}
});
}
openEditModal(id, content) {
this.modalId = id;
this.editModal.querySelector('#textarea-edit').value = content;
this.editModalInstance.open();
}
deletePost(id) {
this.confirmationModalService.content = 'Are you sure you want to delete this post?';
this.confirmationModalService.cta = 'Delete';
this.confirmationModalService.proceed = () => {
this.postsService.deletePost(id).subscribe(data => {
if (data.success) {
this.toastService.success(data.msg);
this.loaderService.isLoading = true;
this.postsService.getPosts().subscribe(response => {
this.postsService.getPostHandling(response);
this.loaderService.hideLoader();
}, err => {
this.toastService.error(err);
return false;
});
} else {
this.toastService.error(data.msg);
}
});
this.authService.decreasePostAmount(JSON.parse(localStorage.getItem('user')).id)
.subscribe(() => {
});
this.confirmationModalService.modalInstance.close();
};
this.confirmationModalService.modalInstance.open();
}
ratePost(id, creator_id) {
this.postsService.ratePost(id, creator_id).subscribe(() => {
if (this.orderSelect === 'post_date') {
this.postsService.getPosts().subscribe(response => {
this.postsService.getPostHandling(response);
}, err => {
this.toastService.error(err);
return false;
});
} else if (this.orderSelect === 'rate') {
this.loaderService.isLoading = true;
this.postsService.getPostsOrdered().subscribe(response => {
this.postsService.getPostHandling(response);
this.loaderService.hideLoader();
}, err => {
this.toastService.error(err);
this.loaderService.hideLoader();
return false;
});
}
});
}
revertRate(id, loggedUser_id) {
this.postsService.revertRatePost(id, loggedUser_id).subscribe(() => {
if (this.orderSelect === 'post_date') {
this.postsService.getPosts().subscribe(response => {
this.postsService.getPostHandling(response);
}, err => {
this.toastService.error(err);
return false;
});
} else if (this.orderSelect === 'rate') {
this.loaderService.isLoading = true;
this.postsService.getPostsOrdered().subscribe(response => {
this.postsService.getPostHandling(response);
this.loaderService.hideLoader();
}, err => {
this.toastService.error(err);
this.loaderService.hideLoader();
return false;
});
}
});
}
isLiked(users_liked) {
return users_liked.indexOf(this.loggedUser) > -1;
}
getCreatorId(post) {
return post.creator[0]['id'];
}
getCreatorName(post) {
return post.creator[0]['name'];
}
getCreatorAvatar(id) {
this.authService.getAvatar(id).subscribe(data => {
this.avatar = data.avatar;
});
}
}
HTML:
<div class="row">
<div class="input-field col s3">
<select id="post-order-select"
[(ngModel)]="orderSelect" name="orderSelect" (change)="order(orderSelect)">
<option value="post_date">Recent posts</option>
<option value="rate">Most rated</option>
</select>
<label>Order by</label>
</div>
</div>
<div id="post-list" class="post-list">
<ng-container *ngIf="!loaderService.isLoading">
<div *ngFor="let post of postsService.posts" class="card-panel first"
[ngClass]="{'own-post' : authService.loggedIn() && getCreatorId(post) === loggedUser}">
<div class="post-details">
<div class="post-header">
<div class="author-avatar"
[ngStyle]="{ 'background-image' : 'url(' + getAvatar + ')'}">
</div>
<div class="post-info">
<div class="author-fullname grey-text text-darken-3">{{ getCreatorName(post) }}</div>
<div class="post-date deep-orange-text">{{ post.post_date | timeago }}</div>
</div>
</div>
<div class="teaser grey-text text-darken-1" [innerHTML]="post.content"></div>
<div class="valign-wrapper rate">
<ng-container *ngIf="authService.loggedIn() && post.creator.id !== loggedUser">
<i *ngIf="!isLiked(post.users_liked)"
class="material-icons tiny deep-orange-text text-lighten-4"
(click)="ratePost(post._id, loggedUser)">thumb_up</i>
<i *ngIf="isLiked(post.users_liked)" class="material-icons tiny deep-orange-text"
(click)="revertRate(post._id, loggedUser)">thumb_up</i>
</ng-container>
<ng-container *ngIf="authService.loggedIn() && post.creator.id === loggedUser">
<i class="material-icons tiny deep-orange-text text-lighten-4 own">
thumb_up
</i>
</ng-container>
<ng-container *ngIf="!authService.loggedIn()">
<i class="material-icons tiny deep-orange-text text-lighten-4 own">
thumb_up
</i>
</ng-container>
<span class="grey-text text-darken-3">{{ post.rate }}</span>
</div>
</div>
<ng-container *ngIf="authService.loggedIn() && post.creator.id === loggedUser">
<a class="btn-floating btn-medium deep-orange darken-3 right"
(click)="deletePost(post._id)">
<i class="material-icons">delete</i>
</a>
<a class="btn-floating btn-medium deep-orange right"
(click)="openEditModal(post._id, post.content)">
<i class="material-icons">create</i>
</a>
</ng-container>
</div>
<div *ngIf="loaderService.isLoadingSmall"
class="row">
<div class="col s12 center-align">
<div class="preloader-wrapper small active">
<div class="spinner-layer">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
</div>
</div>
<div *ngIf="!loaderService.isLoadingSmall && showButton === true" class="row">
<div class="col s12 center-align">
<button (click)="loadMore()" class="btn deep-orange">Load more</button>
</div>
</div>
</ng-container>
</div>
<div *ngIf="loaderService.isLoading" class="row">
<div class="col s12 center-align">
<div class="preloader-wrapper big active">
<div class="spinner-layer">
<div class="circle-clipper left">
<div class="circle"></div>
</div>
<div class="gap-patch">
<div class="circle"></div>
</div>
<div class="circle-clipper right">
<div class="circle"></div>
</div>
</div>
</div>
</div>
</div>
<div id="edit-modal" class="modal">
<div class="modal-content">
<form (submit)="onEditModalSubmit()">
<div class="row">
<div class="col s12">
<div class="input-field deep-orange-text">
<textarea id="textarea-edit" [(ngModel)]="contentEdited" name="contentEdited"
class="materialize-textarea"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col s12">
<button type="submit" class="btn grey darken-4 right">Confirm editing</button>
<a href="#" (click)="editModalInstance.close()" class="btn grey darken-1 right">Cancel</a>
</div>
</div>
</form>
</div>
</div>
最佳答案
声明
let f = someFunc();
将调用 someFunc()
的结果分配给f
。如果您希望 f
成为函数本身的引用,则需要
let f = someFunc;
对函数的引用后跟 ()
或括号内的参数列表是函数调用。
关于javascript - 如何调用分配给带有参数的变量的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53540118/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!