小编Umu*_*zer的帖子

永远不会解决的承诺导致内存泄漏?

我有一个Promise.我创建它以在需要时取消AJAX请求.但是因为我不需要取消那个AJAX,所以我从未解决它并且AJAX成功完成.

一个简化的片段:

var defer = $q.defer();
$http({url: 'example.com/some/api', timeout: defer.promise}).success(function(data) {
    // do something
});

// Never defer.resolve() because I don't need to cancel that ajax. What happens to this promise after request?
Run Code Online (Sandbox Code Playgroud)

永远不会解决这样的承诺导致内存泄漏?您对如何管理Promise生命周期有什么建议吗?

javascript memory-leaks promise angularjs angular-promise

85
推荐指数
1
解决办法
2万
查看次数

确定AJAX是否被取消或在AngularJs拦截器中出错

我有一个"可取消的"angularJs $http调用如下:

var defer = $q.defer()
$http.post("http://example.com/someUrl", {some: "data"}, {timeout: defer.promise})
Run Code Online (Sandbox Code Playgroud)

我取消了ajax请求,defer.resolve()因为某些逻辑需要它.


在我的代码中的其他地方,我有一个像这样的iterceptor:

angular.module("services.interceptor", arguments).config(function($httpProvider) {
     $httpProvider.interceptors.push(function($q) {
        return {
          responseError: function(rejection) {
            if(rejection.status == 0) {
              alert("check your connection");
            }
           return $q.reject(rejection);
        }
      };
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

问题:

如果存在Internet 连接问题,则ajax失败,状态为0,拦截器会捕获它.如果AJAX被取消超时承诺,比状态也为0,并拦截捕获它.

我无法确定它是否被取消或在responseError处理程序中出错.

我天真的方法是检查超时是否定义如下:

responseError: function(rejection) {
  if(rejection.status == 0 && !rejection.config.timeout) {
    alert("check your connection");
  }
  return $q.reject(rejection);
}
Run Code Online (Sandbox Code Playgroud)

它只能保证,还有就是对这项请求超时条件,没有它,因为它的失败.但总比没有好.

是否有确定ajax失败或取消的真正有效方法?

我正在使用AngularJs 1.1.5

angularjs

15
推荐指数
1
解决办法
6668
查看次数

Angular 2和Angularfire2中的三向绑定

我正在尝试使用AngularFire 2(2.0.0-beta.2)将输入元素三路绑定到Angular.js 2(2.0.0-rc.4)中的firebase数据库.

我有一个非常简单的HTML:

<form (ngSubmit)="onSubmit()" #commentForm="ngForm">
  <input [(ngModel)]="model.author" type="input" name="author" required>
</form>
Run Code Online (Sandbox Code Playgroud)

在我的组件中,为了保存和检索此输入的内容到firebase,我有一个这样的实现:

export class CommentFormComponent implements OnInit, AfterViewInit {
  @ViewChild("commentForm") form;
  // http://stackoverflow.com/questions/34615425/how-to-watch-for-form-changes-in-angular-2

  firebaseInitComplete = false;
  model: Comment = new Comment("", "");
  firebaseForm: FirebaseObjectObservable<Comment>;

  constructor(private af: AngularFire) { }

  ngAfterViewInit() {
    this.form.control.valueChanges
      .subscribe(values => {
        // If we haven't get the initial value from firebase yet,
        // values will be empty strings. And we don't want to overwrite
        // real firebase value with empty string on page load …
Run Code Online (Sandbox Code Playgroud)

firebase firebase-realtime-database angularfire2 angular

12
推荐指数
1
解决办法
2475
查看次数

科尔多瓦服务说没有找到404

我刚刚开始使用科尔多瓦.

我已经创建了一个应用程序

cordova create androidTest
Run Code Online (Sandbox Code Playgroud)

然后

cd androidTest

cordova platform add android

cordova prepare

cordova build
Run Code Online (Sandbox Code Playgroud)

最后,

cordova serve android
Run Code Online (Sandbox Code Playgroud)

这说

Static file server running at
  => http://0.0.0.0:8000/
CTRL + C to shutdown
Run Code Online (Sandbox Code Playgroud)

当我打开它时,我唯一得到的是:

404 Not Found
Run Code Online (Sandbox Code Playgroud)

404在科尔多瓦

为什么?我究竟做错了什么?我是否明白了cordova s​​erve命令的含义?

我应该看到在Chrome中"呈现"某种"应用程序",不是吗?

(使用3.1.0-0.1.0)

cordova cordova-3

8
推荐指数
1
解决办法
4135
查看次数

在Angular 2中重定向不匹配的路由

我正在寻找$urlRouterProvider.otherwiseAngular 2中的等价物.

更多细节:

我有这样的路由器定义:

@RouteConfig([
  { path: "/", component: MultiPost, name: "Home", useAsDefault: true },
  { path: "/m/:collectionName/:categoryName/:page", component: MultiPost, name: "MultiPost" }
])
Run Code Online (Sandbox Code Playgroud)

这两条路线都运行得很好,例如当我击中//m/sth/sth/0得到我想要的东西时.

但是,当我点击随机网址时,/kdfsgdshdjf没有任何反应.页面呈现并且<router-outlet>为空,因为没有匹配的路由.

我想要做的是将所有不匹配的路由重定向到默认路由.我需要类似的东西$urlRouterProvider.otherwise("/");.

有谁知道怎么做?

angular2-routing angular

6
推荐指数
2
解决办法
8982
查看次数

在电子中播放本地mp4文件

我正在尝试开发一个小应用程序,我首先通过aperture包捕获屏幕,然后尝试使用video标记在屏幕上显示它.

我捕获屏幕:

import apertureConstructor from 'aperture';
const aperture = apertureConstructor();

const options = {
  fps: 30
};

(async () => {
  await aperture.startRecording(options);
  setTimeout(async () => {
    this.captureUrl = await aperture.stopRecording();
  }, 3000)
})();
Run Code Online (Sandbox Code Playgroud)

请忽略这个烂摊子.Aperture包将捕获的视频写入磁盘,最终,我有了这个文件的路径captureUrl.它是这样的:

/var/folders/29/08gshk3n4mlbbcjnm1m5xyy40000gp/T/tmp-79999m0uOszQK0zaC.mp4

我可以验证此文件是否存在并且播放正常,如果我输入:file:///var/folders/29/08gshk3n4mlbbcjnm1m5xyy40000gp/T/tmp-79999m0uOszQK0zaC.mp4到Google Chrome地址栏.

所以我尝试使用这个地址作为我的video标签的来源,如下所示:

 <video control autoplay>
   <source src="/var/folders/29/08gshk3n4mlbbcjnm1m5xyy40000gp/T/tmp-8004145a2o4gugbVV.mp4" type="video/mp4">
 </video>
Run Code Online (Sandbox Code Playgroud)

哪个抱怨该文件不存在(404):

GET http://localhost:9080/var/folders/29/08gshk3n4mlbbcjnm1m5xyy40000gp/T/tmp-8004145a2o4gugbVV.mp4 404 (Not Found)
Run Code Online (Sandbox Code Playgroud)

是的,它确实试图去,localhost:9080因为在这种情况下它是我的开发服务器主机,并没有这样的文件.

所以我决定添加file://......

<video controls autoplay>
  <source src="file:///var/folders/29/08gshk3n4mlbbcjnm1m5xyy40000gp/T/tmp-8004145a2o4gugbVV.mp4" type="video/mp4">
</video>
Run Code Online (Sandbox Code Playgroud)

这次它说:

Not allowed …
Run Code Online (Sandbox Code Playgroud)

macos html5-video electron

6
推荐指数
2
解决办法
2134
查看次数

如何在 grpc-gateway 中进行 302 重定向

我使用grpc-gateway从我的 proto 定义中托管一个 HTTP 服务器。整体效果很好。

但是,对于一个特殊的端点,我不想返回一个值,而是对 s3 中托管的图像进行 302 重定向。

如果你想通过 grpc-gateway 返回一个错误,你可以像这样返回它

nil, status.Error(codes.Unauthenticated, "Nope")
Run Code Online (Sandbox Code Playgroud)

我想知道是否有类似的东西可以做 302 重定向?

就我从该页面获得的信息而言,似乎不太可能。我希望我忽略了一些东西。

go grpc-gateway

6
推荐指数
2
解决办法
3821
查看次数

在PlayFramework 2.1 JAVA中承诺超时和WS.get Timeout

我试图使用JAVA api在Play Framework 2.1上使用WS访问URL.

这就是我想要的:

  1. 在代码中的某些位置,使用WS.get()(我设置超时1000ms)启动WS请求
  2. 如果WS.get超时或发生了另一个异常,我不希望我的Promise抛出一个异常(因为我的流程以这种方式重新启动)所以我用Promise.recover()另一个来包装那个promise.null在发生故障时返回Reponse对象.
  3. 代码中的其他地方我想"得到"我的响应.我等了5000毫秒,但我得到的是java.util.concurrent.TimeoutException: Futures timed out after [5000 milliseconds]

怎么样? WS.get()timesout后1000毫秒,它抛出,并因为在java.util.concurrent.TimeoutException: No response received after 1000它是由我的恢复功能捕获.它返回null响应而不是异常.

所以诺言最多只能在1000毫秒后"完成".为什么它超时并在5000毫秒后引发异常?

码:

    Logger.info("Fetch started: " + new Date().toString());
Promise<WS.Response> p1 = WS.url("http://athena.ics.forth.gr:9090/RDF/VRP/Examples/tap.rdf").setTimeout(1000).get();
Promise<WS.Response> p2 = p1.recover(new Function<Throwable, WS.Response>() { 
  @Override
  public Response apply(Throwable a) throws Throwable {
    Logger.error("Promise thrown an exception so I will return null. " + new Date().toString() + " …
Run Code Online (Sandbox Code Playgroud)

java promise playframework playframework-2.0

5
推荐指数
0
解决办法
3427
查看次数

确定在控制器中启用了HTML5模式

在Angular Js的控制器中,我想知道是否启用了html5Mode.我怎样才能做到这一点?

细节:

在配置阶段我可以设置$locationProvider.html5Mode(true)或者我可以通过使用来学习启用或不启用html5Mode$locationProvider.html5Mode()

但是,我不能在"运行"块上执行相同的操作,如下所示:

var isHtml5enabled = $location.html5mode() // looking for something like this...
Run Code Online (Sandbox Code Playgroud)

有没有办法做到这一点?

更新

为什么我需要它?

我正在尝试修改ui-router.

ui-routerui-sref指令和$state.href("")服务/方法生成url 而不考虑它是否在Html5Mode中.

如果启用了Html5Mode,那么这样的网址就可以了:/my/base/path/generated/by/uirouter 但是如果它被禁用,它应该是这样的:/my/base/path/#/generated/by/uirouter

所以我试图检测Html5Mode的状态.

我在配置阶段做了,但是在我读完信息后,另一个模块可以改变它的状态.(我猜......)这就是为什么我想确定控制器/运行块上Html5Mode的状态.

angularjs angular-ui

5
推荐指数
2
解决办法
1254
查看次数

仅为Google Chrome中的密码表单启用自动填充功能

假设我有一个这样的登录表单:

 <form>
   <input type="email">
   <input type="password">
 </form>
Run Code Online (Sandbox Code Playgroud)

谷歌浏览器(和其他浏览器)能够提供保存我的电子邮件和密码,没有任何问题.此外,Chrome 在页面加载时自动填充此类表单,没有任何问题.

我需要开发一个功能,通过localstorage"记住"最后一个用户,并显示一些花哨的UI,其中还包括他们的姓名和电子邮件,这次询问密码:

 <div>Welcome our old customer Umut (remembered@email.com)</div> 
 <form>
   <input type="hidden" value="remembered@email.com">
   <input type="password">
 </form>
Run Code Online (Sandbox Code Playgroud)

这次,Chrome不会在页面加载时自动填充密码,但当用户专注于此字段时,他们会先选择用户:

演示

我想知道,如果有办法告诉浏览器,我已经选择了一封电子邮件,并且我想要在页面加载时自动填写该电子邮件的密码.

如果可能,跨浏览器.

PS.如果我不使用hidden的电子邮件类型,而不是只(使用显示:无),使其不可见的用户,Chrome的自动填充这两个电子邮件地址和密码.这意味着,一个不可见的电子邮件文件被Google Chrome"记住"的价值所覆盖.因此,即使您看到remembered@email.com精美的自定义设计,Chrome也会修改电子邮件和密码.

javascript google-chrome remember-me

5
推荐指数
0
解决办法
201
查看次数

Play Framework 2 Scala WS执行同步请求

我正在学习Scala.我以前使用Play Framework 2 Java并尝试使用和学习Scala重写我以前的一些工作.

我需要在我的代码中的某处执行同步WS请求并从中获取Result Object.

当我回到Java时,我常常这样做:

WS.url("someurl").get().get(5000);
Run Code Online (Sandbox Code Playgroud)

或者T Promise<T>.get(Long timeout)确切地说.

自从我切换到Scala后,我现在正在使用play.api.libs.ws并将代码重写为:

val somefuture:Future[Response] = WS.url("someurl").get();
Run Code Online (Sandbox Code Playgroud)

但我不能得到响应未来[回应] syncly!.get()scala上没有方法.

如何ResponseFuture[Response]同步中获取对象?

concurrency scala playframework playframework-2.0

4
推荐指数
1
解决办法
3882
查看次数

RxJs - 仅当有订阅者时才计算和发出值

我想创建一个发出文件添加/删除(通过chokidar)的 observable 。我可以通过这样的方式做到这一点:

Rx.Observable.create((subscriber) => {
  this.watcher = chokidar.watch(
     this.contentPath
  );
  this.watcher.on('addDir', () => { subscriber.next(); });
  this.watcher.on('unlinkDir', () => { subscriber.next(); });
});
Run Code Online (Sandbox Code Playgroud)

我想要做的是,如果没有订阅者,我想停止观看文件,并在订阅时重新开始。像这样的东西,但使用 RxJs:

class Notifier {
  constructor() {
    this.subscriberCount = 0;
  }

  subscribe(onNext, onError, complete) {
    this.subscriberCount++;
    if (this.subscriberCount === 1) {
      this.startInternalWatcher();
    }
    return () => {
      this.subscriberCount--;
      if (this.subscriberCount === 0) {
        this.stopInternalWatcher();
      }
    }
  }
}

// files are not watched
const n = new Notifier(); …
Run Code Online (Sandbox Code Playgroud)

rxjs rxjs5

3
推荐指数
1
解决办法
268
查看次数

在Invertify中将具有属性注入的类绑定到内核

我正在尝试通过inverseify对我的节点项目进行依赖注入。

我有一个这样的内核配置: inversify.config.ts

import "reflect-metadata";
import {Kernel, interfaces} from "inversify";

import {Config} from "./config";
import {Collection} from "./collection";

let kernel = new Kernel();

kernel.bind<Config>("Config").to(Config).inSingletonScope();
// kernel.bind<interfaces.Newable<Collection>>("Collection").toConstructor<Collection>(Collection);
// it works without the line above
Run Code Online (Sandbox Code Playgroud)

一类: config.ts

import {injectable} from "inversify";
@injectable()
export class Config {
  constructor() {}
}
Run Code Online (Sandbox Code Playgroud)

具有属性DI的类 collection.ts

import {injectable} from "inversify";
import getDecorators from "inversify-inject-decorators";
import kernel from "../inversify.config";
let {lazyInject} = getDecorators(kernel);

@injectable()
export class Collection {
  @lazyInject(Config)
  private Config: Config;

  constructor(a: string, b: string) …
Run Code Online (Sandbox Code Playgroud)

dependency-injection typescript inversifyjs

1
推荐指数
1
解决办法
2111
查看次数