如果您想了解将参数传递给JavaScript文件的相关知识,那么本文是一篇不可错过的文章,我们将对js把参数传给第二个参数进行全面详尽的解释,并且为您提供关于javascript–Angular2.将
如果您想了解将参数传递给 JavaScript 文件的相关知识,那么本文是一篇不可错过的文章,我们将对js把参数传给第二个参数进行全面详尽的解释,并且为您提供关于javascript – Angular 2.将参数传递给组件、javascript – Angular2 – 将参数传递给动态生成的组件?、javascript – Angular拖放 – 如何将参数传递给onStart回调函数、javascript – jquery – 将参数传递给函数的有价值的信息。
本文目录一览:- 将参数传递给 JavaScript 文件(js把参数传给第二个参数)
- javascript – Angular 2.将参数传递给组件
- javascript – Angular2 – 将参数传递给动态生成的组件?
- javascript – Angular拖放 – 如何将参数传递给onStart回调函数
- javascript – jquery – 将参数传递给函数
将参数传递给 JavaScript 文件(js把参数传给第二个参数)
通常我会有一个想要使用的 JavaScript 文件,它需要在我的网页中定义某些变量。
所以代码是这样的:
<script type="text/javascript" src="file.js"></script><script type="text/javascript"> var obj1 = "somevalue";</script>
但我想做的是:
<script type="text/javascript" src="file.js?obj1=somevalue&obj2=someothervalue"></script>
我尝试了不同的方法,但最好的方法是像这样解析查询字符串:
var scriptSrc = document.getElementById("myscript").src.toLowerCase();
然后搜索我的价值观。
我想知道是否有另一种方法可以在不构建函数来解析我的字符串的情况下做到这一点。
大家知道其他方法吗?
答案1
小编典典如果可能,我建议不要使用全局变量。使用命名空间和 OOP 将参数传递给对象。
此代码属于 file.js:
var MYLIBRARY = MYLIBRARY || (function(){ var _args = {}; // private return { init : function(Args) { _args = Args; // some other initialising }, helloWorld : function() { alert(''Hello World! -'' + _args[0]); } };}());
在你的 html 文件中:
<script type="text/javascript" src="file.js"></script><script type="text/javascript"> MYLIBRARY.init(["somevalue", 1, "controlId"]); MYLIBRARY.helloWorld();</script>
javascript – Angular 2.将参数传递给组件
<top [mode]="tree">Loading...</top>
在我的组件中,我包括来自angular2 / core的输入
import {Input,Component,OnInit} from 'angular2/core';
在我的组件类中,我声明了一个输入
@input() mode: string;
并且使用console.log()我尝试捕获’tree’的传递参数,但它未定义.
console.log(this,this.mode);
组件文件的完整代码:
import {Http,HTTP_PROVIDERS} from 'angular2/http'; import {Input,OnInit} from 'angular2/core'; import {ParticipantService} from '../services/participant.service'; import {orderBy} from '../pipes/orderby.pipe'; @Component({ selector: 'top',templateUrl: 'dev/templates/top.html',pipes: [orderBy],providers: [HTTP_PROVIDERS,ParticipantService] }) export class AppTopComponent implements OnInit { constructor (private _participantService: ParticipantService) {} errorMessage: string; participants: any[]; @input() mode: string; ngOnInit() { console.log(this,this.mode); this.getParticipants('top3'); var self = this; setInterval(function() { self.getParticipants('top3'); },3000); } getParticipants(public mode: string) { this._participantService.getParticipants(mode) .then( participants => this.participants = participants,error => this.errorMessage = <any>error ); } }
解决方法
因此树必须是父组件中存在的对应于字符串的东西.
如果要使用字符串树,请使用以下命令:
<top mode="tree">Loading...</top>
您可以注意到这些参数不能用于根组件.有关详细信息,请参阅此问题:
> Angular 2 input parameters on root directive
javascript – Angular2 – 将参数传递给动态生成的组件?
我有一个简单的演示应用程序,我正在模拟从DB手动插入/获取数据并注入新组件 – 根据输入的数字.
Plunker
所以如果我点击“手动”按钮两次:
如果我在文本中设置“3”并单击“从数据库中获取” – 我得到预期的延迟(模拟数据库),然后:
这一切都按预期工作.
“父”组件是:
//src/MainPage.ts
@Component({
selector: ''my-app'',
template: `
<button (click)="putInMyHtml()">Insert component manually</button>
<p> # Items to fetch : <input type="text"width:40px'' [(ngModel)]="dbnumItems" name="dbnumItems"/> <input type=''button'' value=''fetch from db'' (click)=''fetchItems($event)''/></p>
<div #myDiv>
<template #target></template>
</div>
`
})
export class MainPage {
@ViewChild(''target'', { read: ViewContainerRef }) target: ViewContainerRef;
dbnumItems: string;
constructor(private cfr: ComponentFactoryResolver) {}
fetchItems(){
var p= new Promise((resolve, reject) => { //simulate db
setTimeout(()=>resolve(this.dbnumItems),2000)
});
p.then(v=>{
for (let i =0;i<v;i++)
{
this.putInMyHtml() ;// inject "v" times
}
})
}
putInMyHtml() {
// this.target.clear();
let compFactory = this.cfr.resolveComponentFactory(TestPage);
this.target.createComponent(compFactory);
}
}
这是Injected组件:
//src/TestPage.ts
@Component({
selector: ''test-component'',
template: ''<b>Content : Name={{user.Name}} Age={{user.Age}}</b><br/>'',
})
export class TestPage {
@Input
User:Person;
}
那问题出在哪里?
如您所见,在注入的组件中,我有:
@Input
User:Person;
这意味着我希望父组件将Person对象传递给每次注入.
换一种说法 :
题
看看“db db stage”之后,我怎样才能将定制人员传递给每次注射?
p.then(v=>{
for (let i =0;i<v;i++)
{
let p = new Person();
p.Name = "name"+i;
p.Age = i;
this.putInMyHtml() ; //how to I pass `p` ???
}
})
}
预期产量:
NB
我不想使用ngFor,因为我不需要在后端持有一个数组.这是一个定期注入新文章的应用程序.我很高兴知道是否有更好的方法.
解决方法:
您可以使用组件引用的实例属性执行此操作,如下所示:
putInMyHtml(p) {
// this.target.clear();
let compFactory = this.cfr.resolveComponentFactory(TestPage);
let ref = this.target.createComponent(compFactory);
ref.instance.user = p;
}
– 修复@input()绑定,语法错误.
– 为模板添加了一个安全导航操作符(?),以便对异步输入进行空检查.
固定式吸油烟机:https://plnkr.co/edit/WgWFZQLxt9RFoZLR46HH?p=preview
总结
以上是小编为你收集整理的javascript – Angular2 – 将参数传递给动态生成的组件?全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
原文地址:https://codeday.me/bug/20190611/1217739.html
javascript – Angular拖放 – 如何将参数传递给onStart回调函数
我有一个使用角度drag and drop的应用程序.
除了一件事,一切都很好.
我需要在onStart回调函数中传递一个参数,但我不知道如何.
我四处搜寻并尝试了几种可能性但没有成功.
函数本身正在工作,已被调用并正确执行,我唯一的问题是将参数传递给它.
代码:
在这个例子中,我做了一个尝试.
<div> <divdata-drop="true" ng-model='todo_list' jqyoui-droppable="{multiple:true,onDrop:'update_item()'}"> <div> <divng-repeat="item in todo_list track by $index" ng-show="item.title" data-drag="true" data-jqyoui-options="{revert: 'invalid'}" ng-model="todo_list" jqyoui-draggable="{index: {{$index}},onStart:'set_board_item_id_panel(event,ui,{board_item_id: item.board_item_id})'}">{{item.title}}</div> </div> </div> </div>
问题:
如何在角度drag and drop的回调函数中传递参数?
非常感谢你!
解决方法
替换……
onStart:'set_board_item_id_panel(event,{board_item_id: item.board_item_id})'
随…
onStart:'set_board_item_id_panel({board_item_id: item.board_item_id})'
然后在您的Controller中执行此操作…
.... $scope.set_board_item_id_panel = function (event,board_item_id) { console.log(board_item_id); } ....
javascript – jquery – 将参数传递给函数
<a href="#" onclick="showmsg('bark')">dog</a> <a href="#" onclick="showmsg('meow')">cat</a>
这是一个javascript函数…
function showmsg(msg) { alert(msg); }
在上面的示例中,可以添加附加内容(html行)而不会破坏行为(javascript).每行html都将自己的参数传递给javascript,告诉它应该显示哪条消息.
如果html改为……
<a href="#">dog</a> <a href="#">cat</a>
那么我该如何编写一个知道单击哪一行html的jquery函数呢?换一种说法…
如何将参数传递给jquery函数?
解决方法
data-
attribute,如下所示:
<a href="#"data-sound="bark">dog</a> <a href="#"data-sound="meow">cat</a>
你的点击处理程序可以获取它,如下所示:
$("a.showmsg").click(function() { alert($(this).attr("data-sound")); });
或者在jQuery 1.4.3中
$("a.showmsg").click(function() { alert($(this).data("sound")); });
You can test it out here.
根据评论澄清:这在HTML5中是完全有效的,在HTML4中它无效,但验证器是您将拥有的唯一问题,它可以在每个HTML4浏览器中使用.如果它在HTML4中没有遗漏,那么它就不会被添加到HTML5中……无论是否验证,我都会亲自使用它.
我们今天的关于将参数传递给 JavaScript 文件和js把参数传给第二个参数的分享就到这里,谢谢您的阅读,如果想了解更多关于javascript – Angular 2.将参数传递给组件、javascript – Angular2 – 将参数传递给动态生成的组件?、javascript – Angular拖放 – 如何将参数传递给onStart回调函数、javascript – jquery – 将参数传递给函数的相关信息,可以在本站进行搜索。
本文标签: