使用角度错误

我正在尝试学习angular,并且在尝试运行此代码时遇到以下错误。

  1. 意外的标记。预期使用构造函数,方法,访问器或属性。
  2. 逗号运算符的左侧未使用且没有副作用。
  3. 需要声明或声明。
export class AppComponent {
  var app = ('app', ['ngAnimate'])

  app.controller('mainCtrl', function($scope) {
    $scope.boxes = [{
      name: 'Friends',
      image: 'https://source.unsplash.com/uAgLGG1WBd4/900x900'
    },{
      name: 'Free',
      image: 'https://source.unsplash.com/Cp-LUHPRpWM/900x900'
    },{
      name: 'Explore',
      image: 'https://source.unsplash.com/7BjmDICVloE/900x900'
    }, {
      name: 'Vast',
      image: 'https://source.unsplash.com/WLUHO9A_xik/900x900'
    }, {
      name: 'Playful',
      image: 'https://source.unsplash.com/b2-fBVrfx0o/900x900'
    }, {
      name: 'Grand',
      image: 'https://source.unsplash.com/Ixp4YhCKZkI/900x900'
    }, {
      name: 'Mist',
      image: 'https://source.unsplash.com/8BmNurlVR6M/900x900'
    }, {
      name: 'Sea',
      image: 'https://source.unsplash.com/6YqpFWWQsno/900x900'
    }, {
      name: 'Reach',
      image: 'https://source.unsplash.com/zFnk_bTLApo/900x900'
    }, {
      name: 'Awe',
      image: 'https://source.unsplash.com/j4PaE7E2_Ws/900x900'
    }, {
      name: 'Surf',
      image: 'https://source.unsplash.com/uohGiEVhWiQ/900x900'
    }, {
      name: 'Thrill',
      image: 'https://source.unsplash.com/ssrbaKvxaos/900x900'
    }, ];

    $scope.selected = [];
    $scope.selectBox = function(item, position) {
      $scope.selected = [{
        item: item,
        position: position
      }];
      $scope.$apply();
    }
    $scope.clearSelection = function() {
      $scope.selected = [];
    }
  })
}

评论
[妄]
[妄]

You have a keyword within the first line of your class: var, var is for assigning scoped variables, inside a class you have properties.

A property is declared in Node.js inside the constructor using the this. keyword,or outside of the constructor (TypeScript).

一个node.js示例:

class AppComponent {
  constructor() {
    this.app = ('app', ['ngAnimate'])
  }
}

我必须承认,我认为这是您遇到的最少问题,尽管我可能错了,但代码似乎无效。

I've never seen app.controller() declared within the main scope of the class, usually this would be expected either inside a method, or as a method declaration.

请随意尝试,进行必要的更改和反馈,以及其他需要解决的问题(因为我几乎可以肯定,您的下一行会遇到问题)。

同时,我将研究AngularJS(以确保不会给您错误的信息),因为我已经习惯于使用Angular 6+和语法,它们是非常不同的框架。

编辑:如果您有链接到您找到此代码的地方/从中学习会大大增加您获得帮助的机会。

点赞
评论