Scala.js 与 angular 类型转换
Scala.js with angular type casting
我正在尝试使用 Scala.JS 和 Angular 实现一个应用程序,但在提交表单时我遇到了类型转换问题。
我有以下代码:
<div ng-controller="usersCtrl">
<form novalidate class="simple-form">
First Name: <input type="text" ng-model="user.firstName" /><br />
Last Name: <input type="text" ng-model="user.lastName" /><br />
E-mail: <input type="email" ng-model="user.email" /><br />
Password: <input type="password" ng-model="user.password" /><br />
ConfirmPassword: <input type="password" ng-model="user.passwordConfirmation" /><br />
<input type="submit" ng-click="controller.signup(user)" value="Save" />
</form>
@JSExport
@injectable("usersCtrl")
class UsersCtrl(
scope: UsersScope,
proxy: UsersServiceProxy,
val timeout: Timeout)
extends AbstractController[UsersScope](scope) {
@JSExport
def signup(user: UserSignup): Future[User] = {
proxy.signup(scope.user)
}
}
@JSExportAll
case class UserSignup(firstName: String, lastName: String, email: String, password: String, passwordConfirmation: String)
但是当我尝试提交表单时出现以下错误:
$c_sjsr_UndefinedBehaviorError {s: "An undefined behavior was detected: [object Object] is not an instance of rentalot.users.UserSignup", e: $c_jl_ClassCastException
我尝试了几种选择,包括通过范围获取用户,将用户变量定义为 UserSignup 或 UndefOf[UserSignup] 但没有成功。后者在我尝试执行 scope.user.toOption.
时失败
有什么想法吗?
Scala.JS 无法自动将 JS 对象转换为 Scala 大小写 class。您需要定义外观。参考this document.
它应该看起来像这样:
@js.native
trait UserSignup extends js.Object {
def firstName: String = js.native
def lastName: String = js.native
...
}
我正在尝试使用 Scala.JS 和 Angular 实现一个应用程序,但在提交表单时我遇到了类型转换问题。
我有以下代码:
<div ng-controller="usersCtrl">
<form novalidate class="simple-form">
First Name: <input type="text" ng-model="user.firstName" /><br />
Last Name: <input type="text" ng-model="user.lastName" /><br />
E-mail: <input type="email" ng-model="user.email" /><br />
Password: <input type="password" ng-model="user.password" /><br />
ConfirmPassword: <input type="password" ng-model="user.passwordConfirmation" /><br />
<input type="submit" ng-click="controller.signup(user)" value="Save" />
</form>
@JSExport
@injectable("usersCtrl")
class UsersCtrl(
scope: UsersScope,
proxy: UsersServiceProxy,
val timeout: Timeout)
extends AbstractController[UsersScope](scope) {
@JSExport
def signup(user: UserSignup): Future[User] = {
proxy.signup(scope.user)
}
}
@JSExportAll
case class UserSignup(firstName: String, lastName: String, email: String, password: String, passwordConfirmation: String)
但是当我尝试提交表单时出现以下错误:
$c_sjsr_UndefinedBehaviorError {s: "An undefined behavior was detected: [object Object] is not an instance of rentalot.users.UserSignup", e: $c_jl_ClassCastException
我尝试了几种选择,包括通过范围获取用户,将用户变量定义为 UserSignup 或 UndefOf[UserSignup] 但没有成功。后者在我尝试执行 scope.user.toOption.
时失败有什么想法吗?
Scala.JS 无法自动将 JS 对象转换为 Scala 大小写 class。您需要定义外观。参考this document. 它应该看起来像这样:
@js.native
trait UserSignup extends js.Object {
def firstName: String = js.native
def lastName: String = js.native
...
}