2016-08-05 4 views
1

Я пытаюсь использовать http с Angular2. Вот мой код:Получить сообщение об ошибке Angular2 http в Es5

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 ТипError: Не удается прочитать свойство 'platform_browser_private 'of undefined

Любые идеи?

FIXED: 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) { 
     ... 
     } 
    }); 

EDIT Смотрите эту Plunker: http://plnkr.co/edit/aQWqxauklT7MqSjfhLFD. Чтобы упростить, я поместил все в файл main.js, а вместо сообщения http я реализовал http get. Однако локально даже почта http работает с API веб-службы. Я надеюсь, что это поможет решить вашу проблему.

+0

по-прежнему та же ошибка – user2705463

+0

Извините, я смутил метод 'isEmailExist' с' emailExistUrl'. Где вы называете метод? – robisim74

+0

От основного компонента. Служба приложений вводится в основной компонент. Он также имеет метод doEmailExist с: return this.applications.doesEmailExist (email); – user2705463

 Смежные вопросы

  • Нет связанных вопросов^_^