Angular V4.0 Several application scenarios

Source: Internet
Author: User
Tags export class port number

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



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.