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 use babel-standalone
I receive unexpected token import
if I try to import it in the following way:
import Player from './player.js';
according to the error that you have. It means that you have problem in JavaScript Polyfill Try this