我看了很多,但我要么找不到答案,要么我不明白.一个具体的例子将赢得投票=)
这是我尝试过的:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
function Ctrl ($scope, $compile) {
$scope.htmlString = htmlString;
}
Ctrl.$inject = ["$scope", "$compile"];
Run Code Online (Sandbox Code Playgroud)
那没用.
我也试过它作为一个指令:
// My magic HTML string function.
function htmlString (str) {
return "<h1>" + str + "</h1>";
}
angular.module("myApp.directives", [])
.directive("htmlString", function () {
return {
restrict: "E",
scope: { content: "@" },
template: "{{ htmlStr(content) }}"
}
});
... and in …Run Code Online (Sandbox Code Playgroud) 我正在研究node.js中的一个程序,它实际上是js.
我有一个变量:
var query = azure.TableQuery...
Run Code Online (Sandbox Code Playgroud)
看起来这行代码没有执行一些时间.
我的问题是:
我怎样才能做到这样的条件:
if this variable is defined do this.
else do this.
Run Code Online (Sandbox Code Playgroud)
我不能在js做 (query!= null)
我想看看这个变量是否被定义做了一些事情.这该怎么做
我怎样才能得到刚刚从一个MySQL表的表有何评论?我尝试了以下内容,但由于各种原因它们无法正常工作.我想弄清楚如何获得字符串'我的评论'(理想情况下通过perl =)
有帮助吗?
-- Abbreviated output for convenience.
SHOW TABLE STATUS WHERE Name="foo"
+------+--------+---------+------------+------+----------------+---------------+
| Name | Engine | Version | Row_format | Rows | Create_options | Comment |
+------+--------+---------+------------+------+----------------+---------------+
| foo | MyISAM | 10 | Fixed | 0 | | my comment |
+------+--------+---------+------------+------+----------------+---------------+
Run Code Online (Sandbox Code Playgroud)
和
SHOW CREATE TABLE foo;
+-------+------------------------------------------------------------------------------+
| Table | Create Table |
+-------+------------------------------------------------------------------------------+
| fooo | CREATE TABLE `fooo` (`id` int(11) NOT NULL PRIMARY KEY) COMMENT='my comment' |
+-------+------------------------------------------------------------------------------+
Run Code Online (Sandbox Code Playgroud) 对于我的生活,我无法获得$ httpBackend来处理执行$ http get请求的控制器.我已经试了几个小时了=)
我把它减少到了下面最简单的形式.如果我,测试通过
也就是说,它是一个有效的,有效的测试和应用程序.
如果我把它重新插入,我会在底部显示错误.
应用程序/ JS/app.js
// Declare a module which depends on filters and services.
var myApp = angular
.module('myApp', ['ngRoute', 'myApp.filters', 'myApp.services',
'myApp.directives'])
.config(['$routeProvider' , function($routeProvider) {
$routeProvider
.when("/dashboard", {
templateUrl: "partials/dashboard.html",
controller: cDashboard
})
.otherwise({redirectTo: "/dashboard"});
}]);
// Pre-define our main namespace modules.
angular.module('myApp.directives' , []);
angular.module('myApp.filters' , []);
angular.module('myApp.services' , []);
angular.module('myApp.controllers', []);
Run Code Online (Sandbox Code Playgroud)
应用程序/ JS/controller.js
function cDashboard ($scope, $http) {
$scope.data = "dog";
// Fetch the actual data.
$http.get("/data") …Run Code Online (Sandbox Code Playgroud) 我有一个班,Foo.我希望能够将构造函数传递给Foo实例,foo并返回相同的实例.
换句话说,我希望这个测试通过:
class Foo; end
foo = Foo.new
bar = Foo.new(foo)
assert_equal foo, bar
Run Code Online (Sandbox Code Playgroud)
谁知道我怎么能这样做?我试过这个:
class Foo
def initialize(arg = nil)
return arg if arg
end
end
foo = Foo.new
bar = Foo.new(foo)
assert_equal foo, bar # => fails
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
救命?
编辑
因为有很多人要求我的理由:
我正在快速分析大量数据(很多TB),我将会有很多对象的实例.对于其中一些对象,使用相同数据的两个不同实例没有意义.例如,一个这样的对象是具有两个属性的"窗口"(如在时间窗口中)对象:开始时间和结束时间.我希望能够以任何这些方式使用构造函数并获取一个窗口对象:
window = Window.new(time_a, time_b)
window = Window.new([time_a, time_b])
window = Window.new(seconds_since_epoch_a, seconds_since_epoch_b)
window = Window.new(window_obj)
window = Window.new(end => time_b, start => time_a)
...
Run Code Online (Sandbox Code Playgroud)
需要窗口的其他一些对象可能会以这种方式实例化:
obj = SomeObj.new(data => my_data, window => window_arg)
Run Code Online (Sandbox Code Playgroud)
我不一定知道window_arg中有什么,我真的不在乎 - 它会接受Window构造函数可以解释的任何单个参数.在已经有Window实例的情况下,我宁愿只使用该实例.但是解释这个问题的工作似乎是Window构造函数的一个问题.无论如何,正如我所提到的,我正在翻阅许多TB数据并创建大量实例.如果窗口对象被传递,我希望它只是被识别为窗口对象并被使用.
我有一个模块要发布到 npm。我发现了一些已有 4 年以上历史的“解决方案”、使用 babel 5.x 的示例,以及其他导致示例无法正常工作的问题。
理想情况下,我想使用 es6 编写我的代码并使用 babel 构建/转换,以便它可以require()在脚本或import模块中导入。
现在,这是我的(示例)模块,显示了我的尝试。
// index.js
export default function speak () {
console.log("Hello, World!");
}
Run Code Online (Sandbox Code Playgroud)
// .babelrc
{
"comments":false,
"presets": [
["@babel/env", {"modules": "commonjs"}]
],
"plugins": [
"@babel/plugin-transform-modules-commonjs",
"add-module-exports"
]
}
Run Code Online (Sandbox Code Playgroud)
// package.json
{
"name": "foo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"build": "babel index.js -d dist"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/cli": "^7.10.5",
"@babel/core": "^7.10.5",
"@babel/plugin-transform-modules-commonjs": "^7.10.4",
"@babel/preset-env": "^7.10.4",
"babel-plugin-add-module-exports": …Run Code Online (Sandbox Code Playgroud) 如果我有一个包含100,000行的文件,如何在指定范围内打印行,例如15010到15020行?
我有这样定义的app:
angular.module("myApp", [...])
.config(function ($stateProvider, $controllerProvider) {
if (isControllerDefined(controllerName)) {
do_stuff();
}
})
Run Code Online (Sandbox Code Playgroud)
控制器以这种方式定义:
angular.module("myApp")
.controller("myController", function ($scope) { ... });
Run Code Online (Sandbox Code Playgroud)
isControllerDefined()如果我有控制器的名称,我如何定义(在上面的配置中)检查给定的控制器是否存在?我觉得我应该做一些像这样的事情:
var ctrl = angular.module("myApp").getController("myController");
var ctrl = $controllerProvider.get("myController");
Run Code Online (Sandbox Code Playgroud)
或类似的东西...但我找不到任何功能.救命?
我有兴趣将AngularJS用于我正在进行的项目.我已经阅读了很多关于它的内容,观看了视频,完成了几个示例应用程序.这一切都有道理,我买入概念.
我正在做的项目需要做一些DOM特效(动态地移动页面等等)并合并一些D3.js图表.
我经常使用jQuery; 我明白并喜欢它.我已经使用了AngularJS来获得基础知识.我完全不明白如何$("#my-element").slideUp()从Angular中调用jQuery这样的东西.例如:
假设我在某个页面中有以下HTML:
<!-- somewhere in app.html -->
<div id="my-element">
<p>Here's some data about your stuff...!</p>
<button id="hider">Hide this (but with a cool transition)</button>
</div>
Run Code Online (Sandbox Code Playgroud)
并在网站JS:
// somewhere in app.js ... pretend it's all nice and DRY.
function main () {
$("#my-element button")
.click(function () { $("#my-element").slideUp()});
}
$(main);
Run Code Online (Sandbox Code Playgroud)
我可以告诉他们如何为Angular完成接近这个的最好的事情是:
HTML
<!-- somewhere in app.html -->
<div ng-controller="Data">
<p>Here's some data about your stuff...!</p>
<button ng-click="slideUp()">Hide this (but with a cool transition)</button>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS:
// somewhere …Run Code Online (Sandbox Code Playgroud) 我有一个角度服务,做一些异步的东西(基于计时器).您可以使用计时器执行的操作之一是定义在计时器到期时触发的"处理程序"(如此伪代码中所示):
flag = false;
timer = new Timer(1000); // ms
timer.handler = function () { flag = true };
Run Code Online (Sandbox Code Playgroud)
在这个简单的情况下,计时器将在1秒后将标志设置为真.我如何用Angular/Karma/Jasmine进行单元测试?
从阅读文档,我希望这可以工作:
...
flag = false;
timer = new Timer(1000);
timer.handler = function () { flag = true };
expect(flag).toBe(false);
sleep(2)
expect(flag).toBe(true);
...
Run Code Online (Sandbox Code Playgroud)
而不是在道德上直立,那个测试决定失败了:
ReferenceError: Can't find variable: sleep
Run Code Online (Sandbox Code Playgroud)
经过一番阅读,显然我不能使用角度场景与Jasmine.好的,我很酷.
更新: 根据评论,我测试了我的"工作"settimeout方法.它永远不会被召唤.
这样可行:
...
flag = false;
timer = new Timer(1000);
timer.handler = function () { flag = true };
expect(flag).toBe(false);
setTimeout(function () { expect(flag).toBe(true) }, 2000);
...
Run Code Online (Sandbox Code Playgroud)
但感觉有点奇怪.
问题: …
angularjs ×5
javascript ×2
node.js ×2
unit-testing ×2
angular-ui ×1
asynchronous ×1
babeljs ×1
comments ×1
config ×1
constructor ×1
controller ×1
controllers ×1
d3.js ×1
html ×1
jasmine ×1
jquery ×1
karma-runner ×1
linux ×1
mysql ×1
npm ×1
ruby ×1
string ×1
transpiler ×1
unix ×1