2017-04-21 26 views
-11


我在Angular Js中有一个警告函数,目前为止只出现在屏幕上,但您可以使用关闭功能关闭它。
这些参数到目前为止称为HTML标签内,我想知道如何将样式应用于它们。例如。在警报主题和警报消息之间进行中断。如何将样式应用于AngularJs中的“警告”功能

这里是代码:
指数

<alert 
    ng-show="showAlert" 
    topic="alertTopic" 
    description="alertMessage" 
    close="closeAlert()"> 
</alert> 

指令:

parking.directive("alert", function() { 
    return { 
    restrict: 'E', 
    scope: { 
topic: '=', 
description: '=', 
close: '&' 
    }, 
    templateUrl: "alert.html", 
    replace: true 
}; 
}); 

应用:

var parking = angular.module("parking", []); 

控制器:

parking.controller("parkingCtrl", function ($scope) { 

$scope.appTitle = "[alert]";  

    $scope.alertTopic = "Something went wrong!"; 
    $scope.alertMessage = "You must inform the plate and the color of the car!" 
    $scope.showAlert = true; 
    $scope.closeAlert = function() { $scope.showAlert = false; }; 
}); 

警报

<div class="alert"> 
    <span class="alert-topic"> 
    <span ng-bind="topic"></span> 
    </span> 
    <span class="alert-description"> 
    <span ng-bind="description"></span> 
    </span> 
    <a href="" ng-click="close()">Close</a> 
</div> 
+12

你的代码,使它看起来像你试过没有,而不是一切... – John3136

回答

4

您正在寻找ArgMin(一参数减少一些功能)。 LINQ的没有ArgMin,但你可以用Aggregate帮助实现它:

int result = testArray.Aggregate((a, s) => Math.Abs(a) < Math.Abs(s) ? a : s); 
+0

这是一个更好的选择,因为它是一个O(N)解决方案。 –

+0

为什么不用'testArray.Min()'来检查数组是否为空? – john

+0

@john:我的目标是展示如何计算* general * case中的'ArgMin'(找到这个项目以使......)。 'testArray.Min(item => Math.Abs​​(item))'在一般情况下不起作用:'[-1,2,3]'应该返回'-1',而不是'1' –

4

在这里你去

int[] testArray = { 4, 3, -10, 4, 3, 0, -2, -5, 8 }; 
int result = testArray.OrderBy(Math.Abs).First(); //in this case 0 is closest to 0 
+1

(数组,然后看起来像{0,-2,3,3,4,-5,8,-10}) –

+0

@DanielFrühauf这是正确的 – fubo

+1

这是O(N)问题的O(N.Log(N))解法。 –