小编Ole*_*leg的帖子

SyntaxError:在严格模式下使用const

我正在使用node.js,并在我正在使用的一个js文件const"strict mode".当试图运行它时,我收到一个错误:

SyntaxError: Use of const in strict mode.
Run Code Online (Sandbox Code Playgroud)

这样做的最佳做法是什么?

编辑:

'use strict'
const MAX_IMAGE_SIZE = 1024*1024; // 1 MB
Run Code Online (Sandbox Code Playgroud)

javascript node.js

238
推荐指数
7
解决办法
24万
查看次数

使用node.js观察文件夹以进行更改,并在更改时打印文件路径

我正在尝试编写一个node.js脚本来监视文件目录中的更改,然后打印更改的文件.如何修改此脚本以便它监视目录(而不是单个文件),并在更改目录时打印文件的名称?

var fs = require('fs'),
    sys = require('sys');
var file = '/home/anderson/Desktop/fractal.png'; //this watches a file, but I want to watch a directory instead
fs.watchFile(file, function(curr, prev) {
    alert("File was modified."); //is there some way to print the names of the files in the directory as they are modified?
});
Run Code Online (Sandbox Code Playgroud)

node.js

77
推荐指数
3
解决办法
5万
查看次数

如何根据端口查找进程并将其全部删除?

根据端口号查找进程并将其全部删除.

ps -efl | grep PORT_NUMBER | kill -9 process_found_previously
Run Code Online (Sandbox Code Playgroud)

如何完成最后一栏?

bash shell

63
推荐指数
6
解决办法
6万
查看次数

AngularJs:使用$ routeProvider调用控制器两次

模块路线:

var switchModule = angular.module('switchModule', []);

switchModule.config(['$routeProvider', function($routeProvider) {
    $routeProvider.
    when('/build-content', {templateUrl: 'build-content.html', controller: BuildInfoController});
}]);
Run Code Online (Sandbox Code Playgroud)

控制器:

function BuildInfoController($http, $scope){
    alert("hello");
}
Run Code Online (Sandbox Code Playgroud)

HTML:

<html ng-app="switchModule">
...
<body>
    <ul>
        <li><a href="#build-content"/></a></li>
    </ul>
    <div class="ng-view"></div>
</body>
...
Run Code Online (Sandbox Code Playgroud)

每次当我点击超链接''时,'BuildInfoController'将被调用两次.我错过了什么吗?

controller angularjs

45
推荐指数
5
解决办法
4万
查看次数

找不到模块"lodash"

今天我尝试了解更多关于Google Web Starter Kit的信息,所以我按照这些说明操作,经过大量的斗争和问题后,我只是尝试启动本地服务器(我们将看到的第一个任务是:$ gulp serve.)并收到此错误:

C:\ gwsk> gulp发球

Error: Cannot find module 'lodash'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\gwsk\node_modules\browser-sync\node_modules\portsc
anner-plus\lib\index.js:3:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\gwsk\node_modules\browser-sync\lib\utils.js:6:19)
Run Code Online (Sandbox Code Playgroud)

老实说,我完全迷失在这里,所以任何帮助都非常受欢迎.我是node.js的新手,为了吞咽,我只是想让它尝试GWSK但是变得头疼:( ...我是网页设计师而不是开发人员......

node.js gulp gulp-sass

40
推荐指数
4
解决办法
14万
查看次数

AngularJS监视数据更改对象的数组

我正在实现购物车,并希望将数据存储在localStorage中.我想观察变量$scope.cart的变化,以便我可以更新localStorage

cart变量如下所示:

[{'name':'foo', 'id':'bar', 'amount': 1 },...]
Run Code Online (Sandbox Code Playgroud)

这是手表的代码.

$scope.updateCart = function(){
    localStorageService.add('cart',JSON.stringify($scope.cart));
    alert('cart updated');
};

$scope.$watch($scope.cart, $scope.updateCart(), true);
Run Code Online (Sandbox Code Playgroud)

这是用户更改模型的HTML.

<li ng-repeat="item in cart">
  {{item.name}} <input type="text" ng-model="item.amount">
</li>
Run Code Online (Sandbox Code Playgroud)

使用以下代码,updateCart()永远不会触发该方法.我不确定我做错了什么.我检查$scope.cart变量确实发生了变化,但未触发更新.

watch angularjs

36
推荐指数
2
解决办法
7万
查看次数

在git init上设置默认目录结构

我想通过自动创建.gitignore,README,LICENSE和其他git init命令文件来优化我的git工作流程.

要做到这一点,我git inithttp://git-scm.com/docs/git-init的 RTFM,它告诉我做以下其中一项:

  1. 使用git init --template=<template_directory>,但这很麻烦.
  2. 更改$ GIT_TEMPLATE_DIR环境变量的内容,但我不愿意.
  3. 设置init.templatedir配置变量.现在我们正在谈论!

所以我sudo mkdir /usr/share/git-core/templates/my_templatetouch它中的一些文件,然后我vim ~/.gitconfig和追加:

[init]
    templatedir = /usr/share/git-core/templates/my_template
Run Code Online (Sandbox Code Playgroud)

git config -l告诉我:

...
init.templatedir =/usr/share/git-core/templates/my_template
...

对自己感到高兴,我去了我的开发游乐场目录,并且:

$ git init
Initialized empty Git repository in /the/current/directory
$ ls -a
.   ..  .git
Run Code Online (Sandbox Code Playgroud)

糟糕......文件在哪里?:(

快速检查:

$ ls -a /usr/share/git-core/templates/my_template
.   ..  .gitignore  LICENSE README.md
$ git --version
git version 1.8.2.1
Run Code Online (Sandbox Code Playgroud)

它似乎$ git init …

git templates

30
推荐指数
3
解决办法
2万
查看次数

导致这种过度"复合层","重新计算样式"和"更新层树"循环的原因是什么?

我对我们的一个webapps中过多的"复合层","重新计算样式"和"更新层树"事件非常感兴趣.我想知道这里是什么造成了他们.

如果您将Chrome指向我们快速移动的流之一,请说https://choir.io/player/beachmonks/github,并启用"FPS计",您可以看到应用程序可以达到大约60fps的大部分我们处于领先地位的时候.

但是,只要我向下滚动几条消息并保持屏幕不变,FPS速率就会急剧下降到10左右甚至更低.代码在这里做的是它呈现每个传入的消息,将其添加到顶部并向上滚动列表Npx,这是新消息的高度,以保持视口位置不变.

(我知道scrollTop会使屏幕无效,但我已经仔细地命令操作以避免布局颠簸.我也知道每秒发生的同步重绘,它是由jquery.sparkline引起的,但它与此讨论无关.)

这是我在尝试描述它时看到的内容. 低fps分析结果.

您认为可能导致大量的图层操作?

performance timeline profiling render google-chrome-devtools

27
推荐指数
3
解决办法
2万
查看次数

Backbone验证不起作用

我使用Backbone的validate函数来保证Man有一个age超过18 的属性.这是我的代码:

var Man = Backbone.Model.extend({
    initialize : function(){
        this.on("error",function(model,error){
            alert(error);
        });
    },
    validate : function(attrs,options){
        if (attrs.age < 18){
            return 'below 18';
        } 
    }
})

var man = new Man({name : 'qian', age : 12});
Run Code Online (Sandbox Code Playgroud)

但看看结果似乎validate不起作用.

javascript backbone.js

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

动态templateUrl - AngularJS

从Angular 1.1.4开始,您可以拥有一个动态模板URL.从这里开始,

templateUrl - 与模板相同,但模板是从指定的URL加载的.由于模板加载是异步的,因此编译/链接将暂停,直到加载模板为止.

您可以将templateUrl指定为表示URL的字符串,或者指定为带有两个参数tElement和tAttrs的函数(在下面的编译函数api中描述)并返回表示url的字符串值.

如何利用它来生成基于我的指令属性的动态模板?显然这不起作用,因为tAttrs.templateType只是字符串"templateType"

templateUrl: function (tElement, tAttrs) {
  if (tAttrs.templateType == 'search') {
    return '/b/js/vendor/angular-ui/template/typeahead/typeahead.html'
  } else {
    return '/b/js/vendor/angular-ui/template/typeahead/typeahead2.html'
  }
}
Run Code Online (Sandbox Code Playgroud)

鉴于我无法访问范围,我该如何管理?

angularjs angular-ui

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