【问题标题】:How to apply styles to an "alert" function in AngularJs [closed]如何将样式应用于AngularJs中的“警报”功能[关闭]
【发布时间】:2017-09-18 04:35:15
【问题描述】:


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

代码如下:
索引

<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>

【问题讨论】:

  • 您的代码让您看起来好像什么都没试过,而不是什么都试过......

标签: javascript css angularjs


【解决方案1】:

您正在寻找 ArgMin(一个参数,它最小化某些功能)。 Linq 没有ArgMin,但是你可以在Aggregate 的帮助下实现它:

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

【讨论】:

  • 还有一个更好的选择,因为它是一个 O(N) 解决方案。
  • 为什么不只是 testArray.Min() 并适当地检查数组是否为空?
  • @john:我的目标是展示如何在 general 的情况下计算ArgMin(找到这样的项目...)。 testArray.Min(item =&gt; Math.Abs(item)) 在一般情况下不起作用:[-1, 2, 3] 应该返回 -1,而不是 1
  • @john 但我们正在寻找最接近零的值,而不是最小值。
  • 只想指出,如果你碰巧使用(相当常见的)MinBy() Linq extension,答案就变成了简单的result = testArray.MinBy(Math.Abs)
【解决方案2】:

给你

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

【讨论】:

  • (数组看起来像 { 0, -2, 3, 3, 4, 4, -5, 8, -10 })
  • @DanielFrühauf 是正确的
  • 这是对 O(N) 问题的 O(N.Log(N)) 解决方案...
  • 与另一个答案相比,由于运行时间而严重否决了正确答案?对于包含 10 个项目的数组,我更关心易于阅读的代码
  • @fubo 我没有投反对票。然而,值得指出的是运行时的问题——不要仅仅因为示例数据只包含 10 个项目,就认为这就是它的全部用途!但我同意它可能完全可以用于预期用途。
猜你喜欢
  • 2020-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 2011-12-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多