ES6中的AngularJS组件

时间:2017-05-23 14:08:04

标签: javascript angularjs ecmascript-6 ngcordova

我在ES6中创建了一个AngularJS组件,如下所示:

<VBox maxHeight="1.7976931348623157E308" maxWidth="1.7976931348623157E308" minHeight="-Infinity" minWidth="-Infinity" prefHeight="162.0" prefWidth="326.0" xmlns="http://javafx.com/javafx/8.0.111" xmlns:fx="http://javafx.com/fxml/1">
    <padding>
        <!--Insets bottom="30.0" left="30.0" right="30.0" top="30.0" /-->
    </padding>
   <children>
      <GridPane hgap="6.0" vgap="6.0" VBox.vgrow="SOMETIMES">
        <columnConstraints>
          <ColumnConstraints halignment="RIGHT" hgrow="NEVER" percentWidth="15.0" />
          <ColumnConstraints hgrow="SOMETIMES" percentWidth="35.0" />
            <ColumnConstraints halignment="RIGHT" hgrow="NEVER" percentWidth="15.0" />
            <ColumnConstraints hgrow="SOMETIMES" percentWidth="35.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <Label text="Left 1" GridPane.halignment="RIGHT" />
            <Label text="Left 2" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
            <Label text="Left 3" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
            <Label text="Right 1" GridPane.columnIndex="2" GridPane.halignment="RIGHT" />
            <Label text="Right 2" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="1" />
            <Label text="Right 3" GridPane.columnIndex="2" GridPane.halignment="RIGHT" GridPane.rowIndex="2" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="1" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="3" />
            <ComboBox maxWidth="1.7976931348623157E308" GridPane.columnIndex="3" GridPane.rowIndex="2" />
         </children>
      </GridPane>
   </children>
</VBox>

我的问题是ComputeLCAInBinaryTreeSpecjava.lang.IndexOutOfBoundsException: Index: 4, Size: 4 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at com.common.BinaryTreeNode.buildBinaryTree(BinaryTreeNode.groovy:62) at com.common.BinaryTreeNode.buildBinaryTree(BinaryTreeNode.groovy:76) at com.common.BinaryTreeNode.buildBinaryTrees_closure1(BinaryTreeNode.groovy:53) at groovy.lang.Closure.call(Closure.java:426) at com.common.BinaryTreeNode.buildBinaryTrees(BinaryTreeNode.groovy:51) at com.elementsofprogramminginterviews.binarytrees.ComputeLCAInBinaryTreeSpec.computes LCA of two nodes of a binary tree_closure1(ComputeLCAInBinaryTreeSpec.groovy:65) at groovy.lang.Closure.call(Closure.java:426) at groovy.lang.Closure.call(Closure.java:442) at com.elementsofprogramminginterviews.binarytrees.ComputeLCAInBinaryTreeSpec.computes LCA of two nodes of a binary tree(ComputeLCAInBinaryTreeSpec.groovy:47) import template from 'template.html' class ctrler { constructor ($scope, $cordovaDevice) { $scope.title = 'This is from component' this.$scope = $scope document.addEventListener('deviceready', this.onDeviceReady) } onDeviceReady() { console.log(this.$scope) } $onDestroy () { document.removeEventListener('deviceready', this.onDeviceReady) console.log('ctrler onDestroy'); } } const cpnt = { template: template, controller: ctrler } export { cpnt } 中的本地参数,但我希望它们成为全局参数,所以我使用$scope,它不起作用

我该怎么办?

2 个答案:

答案 0 :(得分:0)

实际上,$scope不是本地参数,而是全局参数。

onDeviceReady()函数是事件监听器的回调函数,因此this.$scope表示不是ctrler.$scope,这就是问题。

感谢您的回复并阅读我的问题。

祝你有个美好的一天。

答案 1 :(得分:0)

我很确定问题是你如何将回调传递给document.addEventListener()。您希望通过在传递方法时向该方法添加this调用来将bind(this)绑定到控制器的上下文。

试试这个:

document.addEventListener('deviceready', this.onDeviceReady.bind(this));

执行此操作时,this.$scope会在您的回调执行时正确引用ctrler&#39; this.$scope