这个问题是关于在没有抛出异常的情况下在try块之外执行代码的最佳方法.
try {
//experiment
//can't put code after experiment because I don't want a possible exception from this code to be caught by the following catch. It needs to bubble.
} catch(Exception $explosion) {
//contain the blast
} finally {
//cleanup
//this is not the answer since it executes even if an exception occured
//finally will be available in php 5.5
} else {
//code to be executed only if no exception was thrown
//but no try ... …Run Code Online (Sandbox Code Playgroud) 在模板覆盖内容之前,如何从指令中获取input元素?
HTML
<div xxx>
<input a="1" />
</div>
Run Code Online (Sandbox Code Playgroud)
JS
app.directive('xxx', function(){
return {
restrict: 'A',
template: '<p></p>',
replace: true, //if false, just leaves the parent div, still no input
compile: function(element, attrs) {
console.log(element);
return function (scope, iElement, iAttrs) {
}
}
};
});
Run Code Online (Sandbox Code Playgroud)
我在角度为1.0.x,我无法使用'=?传递可选范围参数 语法,我希望能够以非常灵活的方式覆盖指令的默认模板的一部分.而不是每次我计划通过指令时添加范围变量或属性,我希望能够提供要使用的整个元素.
编辑 输入必须保留指令的范围,而不是父节点.
编辑 我试图在指令中包含一个部分模板,该指令将覆盖实际模板的一部分.因此,我所包含的部分需要访问指令的范围而不是父级的.
更新 似乎我没有提供模板或模板URL,而是使用$ templateCache手动替换内容,我可以访问内部元素.我想让角度处理模板和替换,但只是希望能够在它们被替换之前自然地访问指令中的内容.
解决方案 Plunkr
HTML
<body ng-controller="MainCtrl">
<div editable="obj.email">
<input validate-email="error message" ng-model="obj.email" name="contactEmail" type="text" />
</div>
</body>
Run Code Online (Sandbox Code Playgroud)
JS
app.controller('MainCtrl', function($scope) {
$scope.obj = {
email: 'xxx'
};
}); …Run Code Online (Sandbox Code Playgroud) 如何测试循环引用的变量?
我正在使用PHP的var_export()函数,返回字符串参数设置为true.
我发现Warning: var_export does not handle circular references并且想知道是否有人知道测试变量是否包含循环引用的方法,以便我可以在尝试使用它之前使用var_export它.
我知道var_export输出可用于重新创建数组的PHP eval-able文本,即使我没有使用它,我仍然希望在可用时使用此函数,因为输出格式符合我的需要.var_dump不是一个选项,因为它不接受返回字符串的参数.我知道我可以缓冲其输出var_dump优雅地处理循环引用并将缓冲区内容保存到变量但我真的只是想知道是否有人知道在变量中测试此类引用的方法.
如果要创建快速循环引用,请执行以下操作:
$r = array();
$r[] = &$r;
var_export($r, true);
Run Code Online (Sandbox Code Playgroud) 观察:我在Polymer中运行SPA,当我在chrome的新背景标签中打开内部链接时,页面没有完成加载(ajax和all),直到我专注于选项卡.
问题:什么是等待用户关注的chrome -or-什么是Polymer在完成呈现页面和发送ajax请求之前等待的内容?
额外奖励:我如何打开一个开发工具面板,其目标是我在后台打开的标签,以观察/未发生的情况?
奖励答案:如果您在选项卡上打开开发工具,导航离开选项卡,然后刷新开发工具,相关选项卡也将刷新并模拟已在背景中打开.
在 Javascript 中,如何附加事件处理程序以便它在默认操作后运行?
<form target="_blank" action="submit.php" onsubmit="doThisAfterSubmit()">
<input type="submit" onclick="doThisAfterSubmit()" />
</form>
Run Code Online (Sandbox Code Playgroud)
类似但不充分的 Stack Overflow 问题:
javascript:可以添加在默认行为之后运行的事件处理程序吗?
我已阅读这些内容,但它们正在处理通过使用诸如keyupover之类的变体来解决的击键事件keypress。我见过的唯一其他解决方案是该setTimeout()方法。我想避免使用setTimeout()更优雅的解决方案(如果存在)。
我的用例是我想在提交后从 DOM 中删除表单。
我看到很多show_panel用这样的args对象调用这个函数:
{
"keys": ["ctrl+shift+f"],
"command": "show_panel",
"args": {"panel": "find_in_files"}
}
Run Code Online (Sandbox Code Playgroud)
我找不到show_panel函数的定义,并开始认为它没有暴露.是否可以定义新面板?
我想给一组控制器访问特征中定义的方法和属性.现在我提出的最好的实现是:
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope, CtrlTrait) {
$scope.name = CtrlTrait.presetName;
CtrlTrait.setGreeting.call($scope, 'Hello');
});
app.service('CtrlTrait', function() {
this.setGreeting = function(greeting) { this.greeting = greeting; }
this.presetName = 'tom';
});
Run Code Online (Sandbox Code Playgroud)
这很好,但我希望通过控制器的$ scope可以访问属性和方法,而无需在每个控制器中手动创建别名.我希望能够通过将服务注入控制器来使用模板中的属性和方法.
这是可能的,还是我必须创建一个[wrapper around]/[provider for],$scope就像$specialCtrlScope预设我想要的属性和方法一样?
当我创建一个间谍时rootScope,期望因某种原因而失败.检查一下plunkr并尝试将其评论出来然后倒车看看.
describe('Testing', function() {
var rootScope = null
//you need to indicate your module in a test
// beforeEach(module('plunker'));
beforeEach(inject(function($rootScope, $controller) {
rootScope = $rootScope;
rootScope.value = false;
rootScope.testFn = function() {
rootScope.value = true;
}
}));
it('should modify root scope', function() {
// Creating this spy makes test fail
// Comment it outto make it pass
spyOn(rootScope, 'testFn');
expect(rootScope.value).toEqual(false);
rootScope.testFn();
expect(rootScope.value).toEqual(true);
});
});
Run Code Online (Sandbox Code Playgroud) javascript ×4
angularjs ×3
php ×2
events ×1
exception ×1
jasmine ×1
jquery ×1
polymer ×1
sublimetext ×1
sublimetext2 ×1
sublimetext3 ×1
try-catch ×1