1. Http Cross-domain
Unable to access across domains using HTTP, error: "No ' Access-control-allow-origin ' header is present on the requested resource"
The workaround can be added to the server side by adding the Access-control-allow-origin header to the response;
Either use JSONP, or do a reverse proxy on the server side.
2. Background update array, ngfor no update view
Using Ngzone to update the array, refer to: https://stackoverflow.com/questions/31706948/angular2-view-not-changing-after-data-is-updated
Import {Ngzone} from ' Angular2/angular2 ';
Import {Socketservice} from ' Js/services/socketservice ';
Export class MyService {
zone:ngzone;
mylist:array<string> = [];
Socketsvc:socketservice;
Constructor () {
this.zone = new Ngzone ({enablelongstacktrace:false});
This.socketsvc = new Socketservice ();
This.initlisteners ();
}
GetData () {
this.socketSvc.emit (' event ');
}
Initlisteners () {
this.socketSvc.socket.on (' Success ', (data) = {
This.zone.run () =
{ this.mylist = data;
Console.log (' Updated List: ', this.mylist);});}
}
3. Get the URL address of the current page
Path () using the @angular/common/location can only obtain paths outside the host, and does not include the domain name and port number.
You can call Window.location,angular's typescript file directly in your code, and you can directly access the methods and properties of the Window object without import or injection.
such as alert, location, document, etc.
4. URL query string encoding
As above, the method of using window directly encodeuricomponent
Console.log (encodeURIComponent (location.href));
5. Unsafe:url
When using cross-site URI resources, such as pictures, links, etc., using data binding, will automatically be added to the unsafe:
You can use the import {Domsanitizer} from "@angular/platform-browser" to declare a trusted URL.
Reference: https://angular.cn/docs/ts/latest/guide/security.html#! #bypass-security-apis
6. Invoking a third-party JS Library
A) include <script src= in index.html ...
b) Define Xxx.d.ts
c) in the TS file header Add///<reference path= ". /dt/xxx.d.ts "/>
For example, to define a function that can be written in xx.d.ts:
Declare function Jssha (A:any, b:any): void;
Reference: http://www.gyzhao.me/2016/04/30/VsCodeTypings/
Http://www.typescriptlang.org/docs/handbook/namespaces.html