我知道这一点$parse并且$eval都在Angular表达式上运行.那么为什么Angular团队创建了这两个?
我检查了角度库,$eval定义如下:
$eval: function(expr, locals) {
return $parse(expr)(this, locals);
}
Run Code Online (Sandbox Code Playgroud)
那么有什么区别:
$parse(expr)(context, locals);
Run Code Online (Sandbox Code Playgroud)
和
$eval: function(expr, locals) {
return $parse(expr)(this, locals);
}
Run Code Online (Sandbox Code Playgroud)
我想知道哪一个是最佳做法?什么时候使用这两个?
API提供发出/发送回调机制来确认收到的消息.但是,如果断开连接或错误,则不会触发此回调.在我看来,在断开连接时,需要经历一些相当混乱的过程来清理未完成的已发送消息(例如 - 假设您可能希望存储消息以便稍后转发的用例等).关于如何实现这一点的任何简单的想法?想知道我是否遗漏了什么....谢谢.
我有一个输入type=text,我希望显示星星像输入type=password只使用CSS.
基本上我有一个表格,输入如下:
<input type='text' value='hello' id='cake' />
Run Code Online (Sandbox Code Playgroud)
我没有生成表单,我根本无法访问它的HTML.但我可以访问应用于页面的CSS.
我喜欢它的行为就像type=password是 - 显示用户键入的内容而不是输入的实际文本.基本上,我希望那个方面(用户输入的呈现)看起来像一个type=password字段.
由于这似乎只是一个演示问题,我认为必须有一种方法来使用CSS,因为它在其责任域中.但是 - 我还没有找到这样的方法.我必须支持IE8 +,但我宁愿有一个适用于现代浏览器的解决方案,完全没有解决方案.防止复制/粘贴功能的额外点,但我可以没有它.
注意: 如果不清楚,我无法向页面添加HTML或JavaScript - 仅限CSS.
(我发现的只有这个问题,但它正在处理与jQuery相关的问题,它有一个JavaScript解决方案)
作为节点程序员.我习惯使用"nodebacks"来处理我的代码中的错误:
myFn(param, function(err, data) {
if (err){
//error handling logic
}
else {
// business logic
}
});
Run Code Online (Sandbox Code Playgroud)
在编写该函数时,我可以执行以下操作:
var myFn = function(param, callback){
var calc = doSomeCalculation(param);
if(calc === null) { // or some other way to detect error
callback(new Error("error with calculation"), null);
}
...
someAsyncOp(calcN,function(err, finalResult){
if(err) return callback(err, null);
callback(null, finalResult); // the error is null to signal no error
});
};
Run Code Online (Sandbox Code Playgroud)
我如何使用promises进行这种错误处理?
如何在SQL Server中获取当前月份的数量?
一月的手段我想要1和12月我想要12.
所以我在JavaScript中知道允许实现扩展正则表达式语法:
实现可以扩展21.2.1中定义的ECMAScript正则表达式语法,但是它不能扩展下面定义的RegularExpressionBody和RegularExpressionFlags产生或这些产生使用的产品.
这种能力曾被使用过吗?是否有任何现有的JavaScript实现扩展了正则表达式语法?
在一个很好的设计.是否应该在单独的业务逻辑层(在asp.net MVC模型中)处理访问数据库,或者将IQueryables或DbContext对象传递给控制器是否可以?
为什么?各自的优点和缺点是什么?
我正在用C#构建一个ASP.NET MVC应用程序.它使用EntityFramework作为ORM.
让我们稍微简化一下这个场景.
我有一个可爱的蓬松小猫数据库表.每只小猫都有小猫图像链接,小猫蓬松指数,小猫名称和小猫ID.这些映射到EF生成的POCO调用Kitten.我可能会在其他项目中使用此类,而不仅仅是asp.net MVC项目.
我有一个KittenController应该去取最新的蓬松小猫/Kittens.它可能包含选择小猫的一些逻辑,但不是太多的逻辑.我一直在和朋友争论如何实现这一点,我不会透露双方:)
public ActionResult Kittens() // some parameters might be here
{
using(var db = new KittenEntities()){ // db can also be injected,
var result = db.Kittens // this explicit query is here
.Where(kitten=>kitten.fluffiness > 10)
.Select(kitten=>new {
Name=kitten.name,
Url=kitten.imageUrl
}).Take(10);
return Json(result,JsonRequestBehavior.AllowGet);
}
}
Run Code Online (Sandbox Code Playgroud)
public class Kitten{
public string Name {get; set; }
public string Url {get; …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc entity-framework controller separation-of-concerns
我正在使用生成器和Bluebird编写代码,我有以下内容:
var async = Promise.coroutine;
function Client(request){
this.request = request;
}
Client.prototype.fetchCommentData = async(function* (user){
var country = yield countryService.countryFor(user.ip);
var data = yield api.getCommentDataFor(user.id);
var notBanned = yield authServer.authenticate(user.id);
if (!notBanned) throw new AuthenticationError(user.id);
return {
country: country,
comments: data,
notBanned: true
};
});
Run Code Online (Sandbox Code Playgroud)
但是,这有点慢,我觉得我的应用程序等待I/O太多而且它不是并行的.如何提高应用程序的性能?
总响应时间为800 countryFor+ 400 getCommentDataFor+ + 600,authenticate因此总共1800ms这是很多.
在以下代码中,$ q promise 的catch函数捕获异常:
// Fiddle - http://jsfiddle.net/EFpn8/6/
f1().then(function(data) {
console.log("success 1: "+data)
return f2();
})
.then(function(data) {console.log("success 2: "+data)})
.catch(function(data) {console.log("error: "+data)});
function f1() {
var deferred = $q.defer();
// An exception thrown here is not caught in catch
// throw "err";
deferred.resolve("done f1");
return deferred.promise;
}
function f2() {
var deferred = $q.defer();
// An exception thrown here is handled properly
throw "err";
deferred.resolve("done f2");
return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我查看控制台日志输出时,我看到以下内容:

该异常是在Angular中捕获的,但也被浏览器的错误处理所捕获.此行为确实与Q库重现.
这是一个错误吗?我怎样才能真正捕获$ q的异常?
if (function f() {}) {
console.log(f) // Throw an error: f is not defined
}
Run Code Online (Sandbox Code Playgroud)
为什么日志会导致错误,f上面的表达式中还没有定义?
你期望这相当于:
function f () {}
if (true) {
console.log(f); // Throw an error: f is not defined
}
Run Code Online (Sandbox Code Playgroud) javascript ×5
promise ×3
angularjs ×2
bluebird ×2
node.js ×2
asp.net-mvc ×1
async-await ×1
c# ×1
controller ×1
css ×1
generator ×1
html ×1
regex ×1
socket.io ×1
sql-server ×1