我有这样的事情:
$scope.traveler = [
{ description: 'Senior', Amount: 50},
{ description: 'Senior', Amount: 50},
{ description: 'Adult', Amount: 75},
{ description: 'Child', Amount: 35},
{ description: 'Infant', Amount: 25 },
];
Run Code Online (Sandbox Code Playgroud)
现在有了这个数组的总量,我正在做这样的事情:
$scope.totalAmount = function(){
var total = 0;
for (var i = 0; i < $scope.traveler.length; i++) {
total = total + $scope.traveler[i].Amount;
}
return total;
}
Run Code Online (Sandbox Code Playgroud)
当只有一个数组时很容易,但我有其他数组,我想要总结一个不同的属性名称.
我会更高兴如果我可以做这样的事情:
$scope.traveler.Sum({ Amount });
Run Code Online (Sandbox Code Playgroud)
但我不知道如何通过这种方式来解决这个问题,我可以在将来重复使用它,如下所示:
$scope.someArray.Sum({ someProperty });
Run Code Online (Sandbox Code Playgroud)
回答
我决定使用@gruff-bunny建议,所以我避免原型对象(Array)的原型设计
我只是对他的答案做了一点修改,验证了数组,并且sum的值不为null,这是我的最终实现:
$scope.sum = function (items, prop) {
if …Run Code Online (Sandbox Code Playgroud) 我的自定义AuthorizeAttribute遇到了一些问题
public class ExplicitAuthorizeAttribute : AuthorizeAttribute
{
private readonly MembershipUserRole[] _acceptedRoles;
public ExplicitAuthorizeAttribute()
{
}
public ExplicitAuthorizeAttribute(params MembershipUserRole[] acceptedRoles)
{
_acceptedRoles = acceptedRoles;
}
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//Validation ...
}
}
Run Code Online (Sandbox Code Playgroud)
我这样使用它:
[ExplicitAuthorize[(MembershipUserRole.Admin, MembershipUserRole.SuperAdmin)]
Run Code Online (Sandbox Code Playgroud)
它非常适合HttpGet和HttpPost验证我的控制器和方法.
但是当我在ApiController中使用它并进行ajax调用时,AuthorizeCore没有运行,我遇到了安全漏洞.:/
我的枚举看起来像这样
[Flags]
public enum MembershipUserRole
{
Admin= 1,
SuperAdmin = 2
}
Run Code Online (Sandbox Code Playgroud)
有谁知道为什么我的AuthorizeCore在这种情况下没有验证?
顺便说一下如果我用
[Authorized(Roles ="Admin, SuperAdmin")]
Run Code Online (Sandbox Code Playgroud)
这是完美的验证,但我想要Stronly Typed Roles,这就是我使用枚举的原因.
我无法弄清楚何时使用 int、double 和 long。
我正在计算整数的幂并返回结果,只要提供的幂不是负数。
对于作业,我需要使用以下代码开始:
public static long powerN(int number, int power) {
Run Code Online (Sandbox Code Playgroud)
这是我想出的:
public class PowerCalculator
{
/**
* Calculate the non-negative power of an integer number. If a negative power is input, the method returns 1.
*
* @param number The number to take power.
* @param power The power factor to be taken to.
* @return The calculation result after taking power of the integer number.
*/
public static long powerN(int number, int power) {
if …Run Code Online (Sandbox Code Playgroud) ajax ×1
android ×1
api ×1
arrays ×1
c# ×1
cordova ×1
double ×1
int ×1
java ×1
javascript ×1
long-integer ×1
phone-call ×1