1

AngularJS コントローラーの初期化で dart 関数を呼び出そうとしましたが、ReferenceError: myFunction is not defined.

index.dart

import 'dart:js';

myDartFunction() {
  return 42;
}

main() {
  context['myFunction'] = (JsObject exchange) {
    exchange['output'] = myDartFunction();
  };
}

AngularJS を使用した私の html:

<!DOCTYPE html>
<html ng-app="MyApp">
<head lang="en">
    <meta charset="UTF-8">
    <title>My Title</title>
</head>
<body>
    <div ng-controller="MyCtrl">
        <p>{{var}}</p>
    </div>
    <script type="application/dart" src="index.dart"></script>
    <script type="application/javascript" src="../packages/browser/dart.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.26/angular.js"></script>
    <script>
        angular.module("MyApp", []).controller('MyCtrl', function($scope) {
            var exchange = {};
            myFunction(exchange);
            $scope.var = exchange['output'];
        });
    </script>
</body>
</html>

コントローラーが初期化されるとき、それcontext['myFunction']はまだ設定されていないようです。それを待って初期化するにはどうすればよい$scope.varですか?

4

2 に答える 2

1

を使用できることがわかりました。window.onloadこれは、dart 関数が JS にエクスポートされた後に呼び出されます。次に、$scope.$applyを使用してスコープ変数を変更します。

angular.module("MyApp", []).controller('MyCtrl', function($scope) {
    window.onload = function () {
        $scope.$apply(function () {
            var exchange = {};
            myFunction(exchange);
            $scope.var = exchange['output'];
        });
    }
});
于 2014-10-05T11:16:59.693 に答える