admin管理员组文章数量:1321246
I have a template in the ponent with 4 div tags. The JavaScript function I want to call, changeValue()
, is supposed to change the content of the first div from 1
to Yes!
. I'm new to TypeScript and Angular 2, so I'm not sure how I can get the template to interact with the function from dtest2.js
:
/**appponent.ts */
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div id="boolean"><p id="bv">1</p></div>
<div id="numeric"><p id="nv">2</p></div>
<div id="string"><p id="sv">3</p></div>
<div id="enum"><p id="ev">4</p></div>
`
})
export class AppComponent { }
//dtest2.js
function changeValue(){
var newVal= "Yes!";
document.getElementById("bv").innerHTML= newVal;
}
changeValue();
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="file:^html/angular/app/bs.min.js"></script>
<script src="file:^html/dtest2.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>
I have a template in the ponent with 4 div tags. The JavaScript function I want to call, changeValue()
, is supposed to change the content of the first div from 1
to Yes!
. I'm new to TypeScript and Angular 2, so I'm not sure how I can get the template to interact with the function from dtest2.js
:
/**app.ponent.ts */
import { Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `
<div id="boolean"><p id="bv">1</p></div>
<div id="numeric"><p id="nv">2</p></div>
<div id="string"><p id="sv">3</p></div>
<div id="enum"><p id="ev">4</p></div>
`
})
export class AppComponent { }
//dtest2.js
function changeValue(){
var newVal= "Yes!";
document.getElementById("bv").innerHTML= newVal;
}
changeValue();
<html>
<head>
<title>Angular 2 QuickStart</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- Polyfill(s) for older browsers -->
<script src="node_modules/core-js/client/shim.min.js"></script>
<script src="node_modules/zone.js/dist/zone.js"></script>
<script src="node_modules/reflect-metadata/Reflect.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="file:^html/angular/app/bs.min.js"></script>
<script src="file:^html/dtest2.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('app').catch(function(err){ console.error(err); });
</script>
</head>
<body>
<my-app>Loading...</my-app>
</body>
</html>
Share
Improve this question
edited Aug 22, 2016 at 16:07
GʀᴜᴍᴘʏCᴀᴛ
9,02920 gold badges90 silver badges159 bronze badges
asked Aug 22, 2016 at 14:24
xdc17xdc17
731 gold badge2 silver badges7 bronze badges
3 Answers
Reset to default 2Is there a special reason to use this external js function
?
It would be better to use Angular's binding methods to solve your problem..
/**app.ponent.ts */
import { Component } from '@angular/core';
declare function changeValues(anyArgs: string, canBeHere: string) : void;
@Component({
selector: 'my-app',
template: `
<div id="boolean"><p id="bv">{{booleanValue ? 'Yes' : 'No'}}</p></div>
<div id="numeric"><p id="nv">2</p></div>
<div id="string"><p id="sv">3</p></div>
<div id="enum"><p id="ev">4</p></div>
`
})
export class AppComponent {
booleanValue: boolean = true;
constructor() { changeValues(...); }
anyFunctionToChangeTheValue() {
this.booleanValue = false;
}
}
Like the previous answer said, you can do this natively in Angular 2. However, here is how you can access functions outside of Angular2. You just need to declare the "window" object as a constant in your Angular ponent.
//our root app ponent
import {Component, NgModule, OnInit} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
const window: any = {};
@Component({
selector: 'my-app',
template: `
<div id="boolean"><p id="bv">1</p></div>
<div id="numeric"><p id="nv">2</p></div>
<div id="string"><p id="sv">3</p></div>
<div id="enum"><p id="ev">4</p></div>
`
})
export class App implements OnInit {
ngOnInit() {
changeValue();
}
}
@NgModule({
imports: [ BrowserModule ],
declarations: [ App ],
bootstrap: [ App ]
})
export class AppModule {}
Here is a working example.
You can use:
import { Component,ElementRef } from '@angular/core';
constructor(private elementRef:ElementRef) {};
ngAfterViewInit() {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = "./app/custom.js";
this.elementRef.nativeElement.appendChild(s);
}
本文标签: htmlHow to use external Javascript function in Angular 2 templateStack Overflow
版权声明:本文标题:html - How to use external Javascript function in Angular 2 template? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742097353a2420631.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论