Django with Angular 2

后端 未结 4 760
暗喜
暗喜 2020-12-22 21:01

I want to integrate Angular 2 with Django and I have some questions to make.

  1. How can I change the interpolation syntax for Angular 2 from {{ }}

4条回答
  •  一生所求
    2020-12-22 21:59

    Hmm. All the three question I faced recently.

      1. Yes. It is definitely a great idea. Since you have the power of many python libraries as backend to perform whatever action you like combined with the power of angular. :D
      1. Works by injecting your own HTTP-Provider with Updated Default Request Options as Langley suggested. Edit: I recently found a nicer solution using angular2 cookie service. Which injects you CSRSFToken by providing a XSRFStrategy ;-)

    A Drawback is that you require additional libs: NPM:Angular2-cookie

    import { Injectable } from '@angular/core';
    import { CookieService } from 'angular2-cookie/services/cookies.service';
    import { HttpModule, Headers, BaseRequestOptions, RequestOptions, XSRFStrategy, CookieXSRFStrategy }    from '@angular/http';
    
    
    @Injectable()
    export class DefaultRequestOptions extends BaseRequestOptions{
        headers:Headers = new Headers({
            'Content-Type': 'application/json'
        });
    }
    
    @NgModule({
        imports:  [...
            HttpModule],
        declarations: [
            AppComponent, ...,
        ],
        bootstrap: [AppComponent],
        providers: [...
            CookieService,
            {
                provide: RequestOptions,
                useClass: DefaultRequestOptions
            },
            {
                provide: XSRFStrategy,
                useFactory: (cookieService) => {
                    return new CookieXSRFStrategy('csrftoken', 'X-CSRFToken');
                },
                deps: [CookieService]
            }
        ]
    })
    export class AppModule {
        constructor(){
           // ther you go ;-)
        }
    }
    

    static default Interpolation config within your '@angular/compiler' module.

    import { DEFAULT_INTERPOLATION_CONFIG } from '@angular/compiler'
    
    // These values will be used if not provided by your Component.interpolation
    
    DEFAULT_INTERPOLATION_CONFIG.start = '{$';
    DEFAULT_INTERPOLATION_CONFIG.end= '$}';
    

提交回复
热议问题