1

我正在尝试将控制器分配给 ui-router 中的状态。控制器由traceur编译。

状态分配如下所示:

  $stateProvider
    .state('form', {
       url: '/form'
     , templateUrl: 'views/form.html'
     , controller: 'FormController as Form'
   })

编译后的控制器如下所示:

var FormController = function FormController($stateParams, $http) {
this.data = {};
  this._stateParams = $stateParams;
  this._http = $http;
  this.formTemplate = "";
};
($traceurRuntime.createClass)(FormController, {
  submit: function() {
    console.log('submitted');
  }
}, {});

我显然收到了一个运行时错误,它说 FormController 不是一个函数。有办法解决吗?还有其他解决方案吗?

4

1 回答 1

1

工作示例-我只是更改了定义的顺序

var app = angular.module('brokenApp', ['ui.router'])
.config(ApplicationConfig)
; 

// controller def in JS world
var FormController = function ($sc, $stateParams, $http) {
  $sc.title = "hello from scope";
  this.title = "hello from controller";
  this.data = {};
  this._stateParams = $stateParams;
  this._http = $http;
  this.formTemplate = ""; 
}; 

// DI
FormController.$inject = ['$scope', '$stateParams', '$http']

// controller injection into angular
app.controller('FormController', FormController); 

有新的form.html

<h1>Hello Plunker!</h1>
<h3>$scope.title: </h3>
{{title}}

<h3>controller.title: </h3>
{{Form.title}}

而不是这个原始代码

angular.module('brokenApp', ['ui.router'])
.config(ApplicationConfig)
.controller('FormController', FormController);
//# sourceURL=app-wrapper.js
"use strict";
var FormController = function FormController($stateParams, $http) {
  this.data = {};
  this._stateParams = $stateParams;
  this._http = $http;
  this.formTemplate = "";
};

在此处检查工作版本

于 2015-01-14T13:58:05.913 回答