How to import a class in AngularJS 1.6 component controller?

3.2k views Asked by At

What syntax do I have to use if I want to import a class inside a AngularJS 1.6 component controller?

For example, I have the following component.

app/phone-list.component.js

// Register `phoneList` component, along with its associated controller and template
angular.
  module('phonecatApp').
  component('phoneList', {
    template:
        '<ul>' +
          '<li ng-repeat="phone in $ctrl.phones">' +
            '<span>{{phone.name}}</span>' +
            '<p>{{phone.snippet}}</p>' +
          '</li>' +
        '</ul>',
    controller: function PhoneListController() {
      this.phones = [
        {
          name: 'Nexus S',
          snippet: 'Fast just got faster with Nexus S.'
        }, {
          name: 'Motorola XOOM™ with Wi-Fi',
          snippet: 'The Next, Next Generation tablet.'
        }, {
          name: 'MOTOROLA XOOM™',
          snippet: 'The Next, Next Generation tablet.'
        }
      ];
    }
  }); 

And I have a class defined in a separate file, player.js.

player.js

export default class Player {
  constructor () {
    // define properties
  }
  loadVideo () {
    // do stuff
  }
  playVideo () {
    // do stuff
  }
}

I want to use this class inside PhoneListController() like this:

let player = new Player();
player.loadVideo();
player.playVideo();
...

The class works if I put it in the controller file.

I receive unexpected token import if I try to import it in the following way:

import Player from './player.js';
1

There are 1 answers

0
ßastian On BEST ANSWER

according to the error that you have. It means that you have problem in JavaScript Polyfill Try this