LoginSignup
0
0

More than 5 years have passed since last update.

AngularJSのDeveloper GuideのConceptual Overviewのエラー解決

Posted at

Angular 1.3を使うと現時点でDeveloper Guideをそのまま写経すると次のようなエラーが出ます。

AngularJSのDeveloper GuideのConceptual OverviewのAdding UI logic: Controllersをそのまま写経したら

Uncaught SyntaxError: Unexpected token .

Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.7/$injector/modulerr?p0=invoice1&p1=Error%3…Flocalhost%3A8000%2Fbower_components%2Fangular%2Fangular.min.js%3A17%3A350)
のエラーが出てはまってたんですが、調べてみると解決しました。

AngularJS 1.3からはグローバルなコントローラーは認めない

もともとのinvoice1.js
angular.module('invoice1', [])
.controller('InvoiceController', function() {
  this.qty = 1;
  this.cost = 2;
  this.inCurr = 'EUR';
  this.currencies = ['USD', 'EUR', 'CNY'];
  this.usdToForeignRates = {
    USD: 1,
    EUR: 0.74,
    CNY: 6.09
  };

  this.total = function total(outCurr) {
    return this.convertCurrency(this.qty * this.cost, this.inCurr, outCurr);
  };
  this.convertCurrency = function convertCurrency(amount, inCurr, outCurr) {
    return amount * this.usdToForeignRates[outCurr] / this.usdToForeignRates[inCurr];
  };
  this.pay = function pay() {
    window.alert("Thanks!");
  };
});

これを下記のように書き換えてあげるとちゃんと動きます。(最初の2行)

ちゃんと動くinvoice1.js
var app=angular.module('invoice1', [])
app.controller('InvoiceController', function() {
  this.qty = 1;
  this.cost = 2;
  this.inCurr = 'EUR';
  this.currencies = ['USD', 'EUR', 'CNY'];
  this.usdToForeignRates = {
    USD: 1,
    EUR: 0.74,
    CNY: 6.09
  };

  this.total = function total(outCurr) {
    return this.convertCurrency(this.qty * this.cost, this.inCurr, outCurr);
  };
  this.convertCurrency = function convertCurrency(amount, inCurr, outCurr) {
    return amount * this.usdToForeignRates[outCurr] / this.usdToForeignRates[inCurr];
  };
  this.pay = function pay() {
    window.alert("Thanks!");
  };
});

Developer Guideでちゃんと1.3.8の分を見てるのにこれなので初心者にはいきなりの障壁でしたorz

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0