我正在寻找如何绑定到AngularJS中的服务属性的最佳实践.
我已经通过多个示例来了解如何绑定到使用AngularJS创建的服务中的属性.
下面我有两个如何绑定到服务中的属性的示例; 他们都工作.第一个示例使用基本绑定,第二个示例使用$ scope.$ watch绑定到服务属性
在绑定到服务中的属性时,这些示例中的任何一个都是首选的,还是有其他我不知道的选项会被推荐?
这些示例的前提是服务应每5秒更新其属性"lastUpdated"和"calls".更新服务属性后,视图应反映这些更改.这两个例子都成功地运作了 我想知道是否有更好的方法.
基本绑定
可以在此处查看和运行以下代码:http://plnkr.co/edit/d3c16z
<html>
<body ng-app="ServiceNotification" >
<div ng-controller="TimerCtrl1" style="border-style:dotted">
TimerCtrl1 <br/>
Last Updated: {{timerData.lastUpdated}}<br/>
Last Updated: {{timerData.calls}}<br/>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
<script type="text/javascript">
var app = angular.module("ServiceNotification", []);
function TimerCtrl1($scope, Timer) {
$scope.timerData = Timer.data;
};
app.factory("Timer", function ($timeout) {
var data = { lastUpdated: new Date(), calls: 0 };
var updateTimer = function () {
data.lastUpdated = new Date();
data.calls += 1;
console.log("updateTimer: " + data.lastUpdated);
$timeout(updateTimer, 5000); …Run Code Online (Sandbox Code Playgroud) data-binding angularjs angularjs-service angularjs-controller
有没有办法在不知道命名空间的情况下搜索XDocument?我有一个记录所有SOAP请求并加密敏感数据的进程.我想根据名字找到任何元素.有点像,给我所有名称为CreditCard的元素.我不关心命名空间是什么.
我的问题似乎是LINQ并且需要一个xml命名空间.
我有其他从XML检索值的进程,但我知道这些其他进程的命名空间.
XDocument xDocument = XDocument.Load(@"C:\temp\Packet.xml");
XNamespace xNamespace = "http://CompanyName.AppName.Service.Contracts";
var elements = xDocument.Root
.DescendantsAndSelf()
.Elements()
.Where(d => d.Name == xNamespace + "CreditCardNumber");
Run Code Online (Sandbox Code Playgroud)
我真的希望能够在不知道名称空间的情况下搜索xml,如下所示:
XDocument xDocument = XDocument.Load(@"C:\temp\Packet.xml");
var elements = xDocument.Root
.DescendantsAndSelf()
.Elements()
.Where(d => d.Name == "CreditCardNumber")
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为我在编译时事先不知道名称空间.
如何才能做到这一点?
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Request xmlns="http://CompanyName.AppName.Service.ContractA">
<Person>
<CreditCardNumber>83838</CreditCardNumber>
<FirstName>Tom</FirstName>
<LastName>Jackson</LastName>
</Person>
<Person>
<CreditCardNumber>789875</CreditCardNumber>
<FirstName>Chris</FirstName>
<LastName>Smith</LastName>
</Person>
...
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Request xmlns="http://CompanyName.AppName.Service.ContractsB">
<Transaction>
<CreditCardNumber>83838</CreditCardNumber>
<TransactionID>64588</FirstName>
</Transaction>
...
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我使用Ajax.BeginForm异步发布数据到操作.调用该操作但结果将显示到新的网页.我看了很多例子.这似乎并不困难.我已经使这个例子非常简单地用于概念证明(poc),但我仍然看到一个新的页面显示.
调节器
[HttpPost]
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public string TestAjax(UserViewModel viewModel)
{
return viewModel.UserName;
}
Run Code Online (Sandbox Code Playgroud)
视图
@model BasicMvc3Example2.Models.UserViewModel
@{
ViewBag.Title = "Index2";
Layout = null;//"~/Views/Shared/_Layout.cshtml";
}
<script src="/BasicMvc3Example2/Scripts/jquery-1.4.4.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery-ui.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>
<h2>Index2</h2>
<script type="text/javascript">
function PostFailure(){
alert("Failure");
}
function PostSuccess(){
alert("Success");
}
function PostOnComplete() {
alert("Complete");
}
</script>
Page Rendered: @DateTime.Now.ToLongTimeString()
@using (Ajax.BeginForm("TestAjax", new AjaxOptions { HttpMethod = "Post", UpdateTargetId = "textEntered", OnFailure = "PostFailure", OnSuccess = "PostSuccess", OnComplete = "PostOnComplete" …Run Code Online (Sandbox Code Playgroud) 我一直试图解决这个问题大约一个星期了.我在微软网站上发布了这个问题并且只收到了一个回复,但没有解决我的问题.以下是Microsoft网站上我的帖子的组合.
我在Window 7 PC 64位上安装并配置了TFS 2010.当我在浏览器中导航到http:// localhost:8080/tfs时,我明白了
服务不可用 - HTTP错误503.该服务不可用.
任何帮助解决这个问题将不胜感激.
根据之前的阅读,我已经做了很多工作来解决这个问题.以下是我解决这个问题的步骤.列表相当长,但我想确保所有可能与解决此问题相关的信息都可用.
我安装了Team Foundation Server 2010 Power Tools.以下是该工具返回的关键问题
我正在使用Plunker(http://plnkr.co/)来测试JavaScript.从浏览器控制台我想访问我在脚本中定义的变量.
在Plunker中,任何人都可以告诉我如何从浏览器控制台访问变量"someValue".
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<script type="text/javascript">
var someValue = 3
console.log(someValue);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 在我的网站上,我希望用户能够从任何页面登录/注销.当用户选择登录按钮时,将向用户提供模态对话框以供他输入他的凭证.
由于登录将在每个页面上,我想我会为登录创建一个局部视图并将其添加到布局页面.但是当我这样做时,我得到以下错误: 异常详细信息:System.InvalidOperationException:持续时间必须是正数.
还有其他解决方法,不使用部分视图,但我相信这应该工作.
因此,为了测试这一点,我决定使用以下代码使一切变得简单:
使用以下代码创建布局页面
@{Html.RenderAction("_Login", "Account");}
Run Code Online (Sandbox Code Playgroud)
在AccountController中:
public ActionResult _Login()
{
return PartialView("_Login");
}
Run Code Online (Sandbox Code Playgroud)
部分视图_Login
<a id="signin">Login</a>
Run Code Online (Sandbox Code Playgroud)
但是当我运行这个简单版本时,我仍然会收到此错误: 异常详细信息:System.InvalidOperationException:Duration必须是正数.
错误来源指向"@ {Html.RenderAction("_ Login","Account");}"
网上有一些类似于我的问题的对话,它将此识别为MVC的错误(参见下面的链接).但链接属于缓存,我没有做任何缓存.
OuputCache缓存配置文件不适用于子操作
http://aspnet.codeplex.com/workitem/7923
Asp.Net MVC 3部分页面输出缓存不符合配置设置 Asp.Net MVC 3部分页面输出缓存不符合配置设置
使用缓存配置文件缓存ChildActions不起作用? 使用缓存配置文件缓存ChildActions不起作用?
我不确定这是否有所作为,但我会继续在这里添加它.我正在使用带有Razor的MVC 3.
更新
堆栈跟踪
[InvalidOperationException: Duration must be a positive number.]
System.Web.Mvc.OutputCacheAttribute.ValidateChildActionConfiguration() +624394
System.Web.Mvc.OutputCacheAttribute.OnActionExecuting(ActionExecutingContext filterContext) +127
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +72
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314
System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976
System.Web.Mvc.Controller.ExecuteCore() …Run Code Online (Sandbox Code Playgroud) 我正在创建一个概念证明,它将Lambda/LINQ用于动态where和orderby.以下代码适用于where表达式,但我无法弄清楚如何通过表达式创建订单.对于这个例子,如果可能的话我想保持简单; 我宁愿不编写修改表达式树的代码.
void Main()
{
DateTime productSince = DateTime.UtcNow.Subtract(new TimeSpan(1,30,0));
Expression<Func<Products, bool>> filter = d => d.CreatedDate > productSince && d.Price < 100 ;
List<Products> products = GetProducts(filter, Products);
Console.WriteLine(products);
}
private static List<Products> GetProducts(Expression<Func<Products, bool>> filter, Table<Products> Products)
{
var products = Products.Where(filter);
return products.ToList();
}
Run Code Online (Sandbox Code Playgroud)
我想要的是类似于以下内容,但无法弄清楚通过表达式创建顺序的代码.
void Main()
{
DateTime productSince = DateTime.UtcNow.Subtract(new TimeSpan(1,30,0));
Expression<Func<Products, bool>> filter = d => d.CreatedDate > productSince && d.Price < 100 ;
Expression<Func<Products, ????>> orderBy = d => ??????;
List<Products> products = …Run Code Online (Sandbox Code Playgroud) 我希望能够在用户选择"GO"按钮时在客户端触发表单验证.目前,当选择"GO"按钮时,它不会验证完整的表格.例如,如果我加载表单并选择"Go"按钮而不将焦点放在文本框中,则不会验证任何内容,并且val.Valid()返回true.如果我在文本框中输入了无效数据,则会对该单个项目进行验证; 当我选择"GO"按钮时,val.Valid()返回false.
所以我想要做的是,当用户选择一个按钮或其他事件时,我想触发所有表单验证.
我在MVC 3中这样做
public class TestValidationModel
{
[Required(ErrorMessage="UserName Is Required")]
[RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
[StringLength(12, MinimumLength = 3)]
public string UserName { get; set; }
[Required(ErrorMessage="Password Is Required")]
[StringLength(20, MinimumLength = 3)]
public string Password { get; set; }
[Required(ErrorMessage="Email Address Is Required")]
[Display(Name = "Email Address")]
public string EmailAddress{ get; set; }
}
<script src="/BasicMvc3Example2/Scripts/jquery-1.4.4.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery-ui.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.js" type="text/javascript"></script>
<script src="/BasicMvc3Example2/Scripts/jquery.validate.unobtrusive.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
$("#butValidateForm").click(function(){
var val …Run Code Online (Sandbox Code Playgroud) 我有一个模型类,它有一个描述属性,数据注释属性为StringLength,长度设置为100个字符.当此属性超过100个字符并且实体框架尝试保存此属性时,我收到以下错误.
[StringLength(100, ErrorMessage = "Description Max Length is 100")]
public string Description { get; set; }
Run Code Online (Sandbox Code Playgroud)
错误:
"一个或多个实体的验证失败.有关详细信息,请参阅'EntityValidationErrors'属性"
我不确定这是否有助于形成解决方案,但我正在使用Entity Framework CTP5和Code First.
我想要做的是,如果描述超过100个字符,然后删除超过100个字符的字符,以便可以存储描述并且不会引发错误.
我相信我应该能够手动使用DataAnnotation属性StringLength来帮助我识别有效的描述长度,然后使用SubString删除有效数量上的任何字符.
有谁知道在这种情况下如何使用DataAnnotation?或者还有其他选择吗?
更新 我做了BrokenGlass建议的内容,在这里我的实现如果:
public static class DataAnnotation
{
public static int? GetMaxLengthFromStringLengthAttribute(Type modelClass, string propertyName)
{
int? maxLength = null;
var attribute = modelClass.GetProperties()
.Where(p => p.Name == propertyName)
.Single()
.GetCustomAttributes(typeof(StringLengthAttribute), true)
.Single() as StringLengthAttribute;
if (attribute != null)
maxLength = attribute.MaximumLength;
return maxLength;
}
}
int? maxLength = DataAnnotation.GetMaxLengthFromStringLengthAttribute(typeof(Car), "Description");
if(maxLength != null && car.Description.Length …Run Code Online (Sandbox Code Playgroud) 是否有工具,流程或解决方案将以下LINQ查询语法转换为使用Lambdas的方法语法(点表示法)?我希望解决方案将以下查询语法转换为方法语法,例如this.
var filteredEmployees =
from employee in allEmployees
where employee.DepartmentID < 4 && employee.EmployeeID < 10
orderby employee.DepartmentID descending,
employee.LastName descending
select employee;
Run Code Online (Sandbox Code Playgroud)
以下
var filteredEmployees2 = allEmployees.Where(employee => ((employee.DepartmentID < 4) && (employee.EmployeeID < 10)))
.OrderByDescending(employee => employee.DepartmentID)
.ThenByDescending(employee => employee.LastName);
Run Code Online (Sandbox Code Playgroud)
我想用它来更好地学习方法语法.
c# ×3
asp.net-mvc ×2
lambda ×2
linq ×2
.net ×1
angularjs ×1
code-first ×1
data-binding ×1
javascript ×1
jquery ×1
linq-to-xml ×1
plunker ×1
razor ×1
tfs ×1
tfs2010 ×1