有没有办法在Typescript类中指定类型安全的可选成员?
就是这样......
class Foo {
a?: string;
b?: string;
c: number;
}
....
foo = new Foo();
...
if (foo.a !== undefined) { ... (access foo.a in a type-safe string manner) ... }
Run Code Online (Sandbox Code Playgroud)
如果您熟悉OCaml/F#,我正在寻找类似'string option'的东西.
我在用于登录的部分页面中有以下代码...
<div class="container">
<form autocomplete="on" data-ng-submit="checkCredentials()" class="form-signin">
<h2 class="form-signin-heading">Login page</h2>
<label for="email"> <b>User e-mail</b> </label>
<input required autocomplete="on" name="email" type="text" data-ng-model="modelLogin.email" class="input-block-level" placeholder="Email address">
<label for="pass"> <b>Password</b> </label>
<input required autocomplete="on" name="pass" type="password" data-ng-model="modelLogin.password" class="input-block-level" placeholder="Password">
<button style="margin-bottom:1em" class="btn btn-large btn-primary" type="submit">Log in</button>
</form>
</div>
Run Code Online (Sandbox Code Playgroud)
...这是我的唯一主页(index.html)中包含的ng:
<body data-ng-controller="controllerLogin">
<div data-ng-include="getMainpageBasedOnLoginStatus()">
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
...与getMainpageBasedOnLoginStatus()函数返回适当的路径要么对泛音/ loginPage.html上面所示或所述的partials/mainApplicationPage.html(我的主要的应用程序的入口点).通过ng-submit完成的checkCredentials()调用执行webservice调用以获取登录成功/失败,并在全局模型变量中进行正确更新 - 以便后续调用getMainpageBasedOnLoginStatus()返回'ok-you-are-登录页面(partials/mainApplicationPage.html).
此登录方案不能被黑客入侵,因为即使客户端代码以某种方式更改为指向主应用程序部分而不是登录部分,服务器端也将无法正常运行(Web服务请求将无法正常运行并返回406错误).
一切都很好 - 它的工作原理.除了...自动填充.
登录/密码字段既不会自动完成,也不会自动填充Chrome 29.在Firefox 23中也是如此:登录/密码字段未自动填充,但登录名(仅登录名,而不是密码名!)是自动填充的 - …
当我检测到特定的客户端错误时,我只是尝试设置(实际上是通过将最大年龄设置为0来删除)会话cookie。我正在使用的HTTP响应来自4xx类别(例如401、406等)。
Cookie删除与服务器端生成的这种响应配合得很好:
Response resp = Response.status(Response.Status.OK).header(
"Set-Cookie",
cookieName+"="+sessionId+"; "+
"Version=1; Max-Age=0; Path=" + cookiePath + "; " +
"Expires=Thu, 01 Jan 1970 00:00:00 GMT").entity("").build();
Run Code Online (Sandbox Code Playgroud)
...但是失败了:
Response resp = Response.status(Response.Status.UNAUTHORIZED).header(
"Set-Cookie",
cookieName+"="+sessionId+"; "+
"Version=1; Max-Age=0; Path=" + cookiePath + "; " +
"Expires=Thu, 01 Jan 1970 00:00:00 GMT").entity("").build();
Run Code Online (Sandbox Code Playgroud)
(仅相差:200 => 406)。
不能使用4xx响应设置cookie吗?