我有以下angularjs代码:
$scope.clients = commonFactory.getData(clientFactory.getClients());
if ($scope.clients.length > 0) {
$scope.sampleForm.ClientId = $scope.clients[0].ClientId;
}
Run Code Online (Sandbox Code Playgroud)
而commonFactory中的getData函数:
factory.getData = function (method) {
method.then(function (response) {
return response.data;
}, function (error) {
$rootScope.alerts.push({ type: 'error', msg: error.data.ExceptionMessage });
});
};
Run Code Online (Sandbox Code Playgroud)
问题是,由于异步调用,$ scope.clients.length在遇到该行时未定义.
有没有办法不进行长度检查,直到我知道已经分配了$ scope.clients?我看过这样的事情:
$scope.clients = commonFactory.getData(clientFactory.getClients()).then(function () {
if ($scope.clients.length > 0) {
$scope.sampleForm.ClientId = $scope.clients[0].ClientId;
}
});
Run Code Online (Sandbox Code Playgroud)
试图链我的then承诺,但没有骰子......这里的目标是让GetData方法,以避免一堆烦人的代码捕获错误...也许我要对此错了吗?
我有以下多选框:
<select
multiple="multiple"
data-ng-model="rightSelected"
data-ng-options="slide as slide.SlideBarcode for slide in form.Slides"
data-ng-required="form.Slides.length > 0"
/>
Run Code Online (Sandbox Code Playgroud)
在我的控制器中,我在初始化时这样做:
$scope.form.Slides = [];
Run Code Online (Sandbox Code Playgroud)
如果Slide数组中有幻灯片,我希望表单的这个元素有效.这些是动态添加的 - 它是用户可以添加幻灯片的幻灯片.
但是我没有得到ngRequired的东西如何工作......如果我改变它data-ng-required="true"然后我的表格看起来没问题,但当然它不能做我想要的.如果我使用的form.Slides.length > 0是检查我真正想要的东西,不仅这不起作用,而且它与我的表格结构相混淆,我的一个元素似乎随意消失.
使用它的正确方法是什么?这个文档非常稀少.
我的Web.API路由有问题.我有以下两条路线:
config.Routes.MapHttpRoute(
name: "MethodOne",
routeTemplate: "api/{controller}/{action}/{id}/{type}",
defaults: new { id = RouteParameter.Optional, type = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "MethodTwo",
routeTemplate: "api/{controller}/{action}/{directory}/{report}",
defaults: new { directory = RouteParameter.Optional, report = RouteParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
在我的控制器中这两种方法:
[HttpGet]
[ActionName("methodone")]
public string MethodOne(string id, string type)
{
return string.Empty;
}
[HttpGet]
[ActionName("methodtwo")]
public string MethodTwo(string directory, string report)
{
return string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
这两个看似不能并存.如果我在WebApiConfig中注释掉MethodOne路由,则MethodTwo路由可以工作.注释MethodTwo路由允许MethodOne工作.留下两个未注释的,MethodOne可以工作,但不是MethodTwo.
我希望为这两个路由使用一个路由,然后它们似乎必须具有相同的参数名称.谁用通用参数名称编写方法?坏.我真的不希望我的方法具有相同的参数名称(如p1,p2,p3),所以我想我可以为新方法创建一个路径.但即使这样似乎也行不通.
我真的很想念WebGet(UriTemplate="")WCF的休息.
我有一个控制器有很多方法,一些有1,2,3或甚至更多的参数.我无法相信我无法使用MapHttpRoute方法使用有意义的参数名称.
我可以完全评论那些东西并使用WebGet()......但在我到达那里之前我想看看我是否遗漏了一些东西.
我似乎无法让它工作......我在客户端上有一些像这样的jQuery:
$.ajax({
type: "POST",
url: "api/report/reportexists/",
data: JSON.stringify({ "report":reportpath }),
success: function(exists) {
if (exists) {
fileExists = true;
} else {
fileExists = false;
}
}
});
Run Code Online (Sandbox Code Playgroud)
在我的Web.API控制器中,我有一个这样的方法:
[HttpPost]
public bool ReportExists( [FromBody]string report )
{
bool exists = File.Exists(report);
return exists;
}
Run Code Online (Sandbox Code Playgroud)
我只是检查一个文件是否存在于服务器上,并返回一个bool是否存在.我发送的报告字符串是UNC路径,因此reportpath看起来像'\\ some\path \'.
我可以解决脚本问题,并在ReportExists方法中点击断点,但报表变量始终为null.
我究竟做错了什么?
我也看到了使用.post和postJSON发布的方法.也许我应该使用其中之一?如果是这样,我的格式是什么?
更新: 一个额外的线索可能 - 如果我删除[FromBody]然后我的断点根本没有被击中 - '没有找到匹配请求的http资源'.我看的例子表明不需要[FromBody] ......?
我认为这很简单,但我似乎无法找到正确的选择器语法.我有这样的HTML:
<div class="parent">
<h2>Summary</h2>
<p>... bunch of content ...</p>
</div>
<div class="parent">
<h2>Statistics</h2>
<p>... bunch of content ...</p>
</div>
<div class="parent">
<h2>Outcome</h2>
<p>... bunch of content ...</p>
</div>
Run Code Online (Sandbox Code Playgroud)
我想找到其h2子包含文本'Statistics'的div.
我尝试了各种组合:
$(".parent").find("h2[text='Statistics'").parent()
Run Code Online (Sandbox Code Playgroud)
这不起作用,因为find返回一个空集.如果我这样做:
$(".parent").find("h2").text("Statistics").parent()
Run Code Online (Sandbox Code Playgroud)
我找回了3个元素 - 所有这些元素似乎都是包含统计数据的div.我可以添加first(),但看起来有点粗糙.找到一个项目的正确语法是什么?
看起来很简单,但没有正确的语法.我想知道我的s3存储桶中是否存在使用通配符的文件.就像是
aws s3 ls s3://my-bucket/folder/*myfile*
Run Code Online (Sandbox Code Playgroud)
目标是查看此存储桶中是否存在被调用2016_myfile.txt的文件或调用的文件2011_myfile.csv.
如果我运行该命令,即使我知道该文件存在,它也不会返回任何内容.
鉴于此代码(来自其他人):
var module = angular.module('myApp', []);
module.controller('MyCtrl', function ($scope){
$scope.json = JSON.stringify({a:1, b:2});
});
module.directive('myDownload', function ($compile) {
return {
restrict:'E',
scope:{ data: '=' },
link:function (scope, elm, attrs) {
function getUrl(){
return URL.createObjectURL(new Blob([JSON.stringify(scope.data)], {type: "application/json"}));
}
elm.append($compile(
'<a class="btn" download="backup.json"' +
'href="' + getUrl() + '">' +
'Download' +
'</a>'
)(scope));
scope.$watch(scope.data, function(){
elm.children()[0].href = getUrl();
});
}
};
});
Run Code Online (Sandbox Code Playgroud)
小提琴示例可以在Chrome中下载.但是单击"下载"链接在IE11中没有任何作用.没有错误,没有警告,没有任何响应.
但根据这个它在IE10和11.1的支持.
是否有一些需要更改的IE安全设置或正在进行的操作?
简单的问题 - 如果我在我的控制器中有这个:
$scope.somedata = 'Item 1, Item 2'; // set from something else
Run Code Online (Sandbox Code Playgroud)
有没有办法在ngRepeat表达式中为我的视图拆分somedata?就像是:
<div ng-repeat="item in somedata | split(',')">{{item}}</div>
Run Code Online (Sandbox Code Playgroud)
哪个不起作用......
或者我必须这样做
$scope.somedata = 'Item 1, Item 2'; // set from something else
$scope.items = somedata.split(',');
Run Code Online (Sandbox Code Playgroud)
然后
<div ng-repeat="item in items>{{item}}</div>
Run Code Online (Sandbox Code Playgroud)
我想我并没有真正理解我在AngularJS中的表达式中能做些什么.
在wpf中做一些3D东西 - 想要使用更简单的测试来查看一切是否正常(在移动到曲线之前).
基本问题给出了两个点x1,y1,z1和x2,y2,z2我计算了点之间的距离.但是如何在某个距离找到位于该线上的另一个点(x3,y3,z3)的坐标?
也就是说,如果我的线在-50,0,0和50,0,0之间的长度为100,那么沿着线100*0.1处的点的坐标是多少?
我认为这是一个简单的公式,但我还没有找到它......
我在MVVM-Light包中看到我可以用令牌发送消息 - 我需要做的是发送一个对象,并在该对象上附加一条消息,如添加,编辑,删除等等.
发送和接收此消息的最佳方式是什么?我认为发送它只是:Messenger.Default.Send(myObject,ActionEnum.DELETE);
但在接收:Messenger.Default.Register(this,????,HandleMyMessage);
什么是正确的语法?
谢谢!