2016-08-05 54 views
1

我正在嘗試使用http和Angular2。 這裏是我的代碼:在Es5中獲取Angular2 http的錯誤

var _domain = 'http://localhost:3000/'; 

    app.Applications = ng.core.Injectable().Class({ 
     constructor: [ng.http.Http, function(http) { 
     this.http = http; 
     this.emailExistUrl = _domain + 'api/applications/email'; 
     }], 

     doesEmailExist: function(email) { 
     var data = { email: email }; 
     return this.http.post(this.emailExistUrl, data) 
      .toPromise() 
      .then(function(response) { response.json().data; }) 
      .catch(this.handleError); 
     } 
    }); 

主要成分:

app.AppComponent = ng.core 
    .Component({ 
    selector: 'register-form', 
    templateUrl: 'src/register/app.component.html', 
    providers: [app.Applications] 
    }) 
    .Class({ 
    constructor: [ng.core.ElementRef, app.Applications, function(ref, Applications) { 
     this.programs = JSON.parse(ref.nativeElement.getAttribute('programs')); 
     this.applications = Applications; 
    }], 
    doesEmailExist: function(email) { 
     return this.applications.doesEmailExist(email); 
    } 
    }); 

這裏main.js文件:

document.addEventListener('DOMContentLoaded', function() { 
    ng.platformBrowserDynamic.bootstrap(app.AppComponent, [ 
     ng.forms.disableDeprecatedForms(), 
     ng.forms.provideForms(), 
     ng.http.HTTP_PROVIDERS, 
    ]); 
    }); 

當doesEmailExist叫我從HTTP模塊得到一個錯誤:

vendor-client.min.js:55470 TypeError:無法讀取屬性'platform_browser_private'的undefined

任何想法?

修復: Http在腳本標記列表上的平臺瀏覽器之前。 :/

<script src="https://npmcdn.com/@angular/http/bundles/http.umd.js"></script> 
<script src="https://npmcdn.com/@angular/platform-browser/bundles/platform-browser.umd.js"></script> 
<script src="https://npmcdn.com/@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js"></script> 

逆更好:)

回答

1

嘗試在構造函數中的開頭分配http

app.Applications = ng.core.Injectable().Class({ 
     constructor: [ng.http.Http, function(http) { 
     this.http = http; 
     ... 
     }], 

     doesEmailExist: function(email) { 
     ... 
     } 
    }); 

編輯 看到這個Plunker:http://plnkr.co/edit/aQWqxauklT7MqSjfhLFD。爲了簡化,我已將所有內容放在main.js文件中,而不是http後我實現了http get。但是,在本地,甚至http post也可以使用web服務API。我希望這有助於解決您的問題。

+0

仍然是一樣的錯誤 – user2705463

+0

對不起,我把'doesEmailExist'方法與'emailExistUrl'混淆了。你在哪裏叫這個方法? – robisim74

+0

來自主要組件。應用程序服務注入主要組件。它也有一個doesEmailExist方法:return this.applications.doesEmailExist(email); – user2705463