Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AngularJS中输入类型编号_Javascript_Angularjs_Validation_Ng Class - Fatal编程技术网

Javascript 在AngularJS中输入类型编号

Javascript 在AngularJS中输入类型编号,javascript,angularjs,validation,ng-class,Javascript,Angularjs,Validation,Ng Class,我在AngularJS中有一个输入类型编号,如下所示 <input type="number"></input> 我希望实现以下目标: 如果用户输入正值,则应显示相同的字体和颜色 如果用户显示负值,则应以红色显示字体颜色,表示为负数 有人能告诉我如何动态实现这一点吗? <input type="number" ng-change="amountChanged()" ng-style="myStyle.color" ng-model="amount"/>

我在AngularJS中有一个输入类型编号,如下所示

<input type="number"></input>

我希望实现以下目标:

  • 如果用户输入正值,则应显示相同的字体和颜色

  • 如果用户显示负值,则应以红色显示字体颜色,表示为负数

有人能告诉我如何动态实现这一点吗?


<input type="number" ng-change="amountChanged()" ng-style="myStyle.color" ng-model="amount"/>

$scope.myStyle= {};
$scope.amountChanged = function () {

   if($scope.amount < 0)
   {
      $scope.myStyle.color= {"color":"green"};
   }
   else
   {
      $scope.myStyle.color= {"color":"blue"};
   }

}
$scope.myStyle={}; $scope.amountChanged=函数(){ 如果($scope.amount<0) { $scope.myStyle.color={“color”:“green”}; } 其他的 { $scope.myStyle.color={“color”:“blue”}; } }
希望能有所帮助。


  <input type="number" ng-model="number">
    <label ng-show="number > 0" style="color:green;">You are Correct</label>
    <label ng-show="number < 0" style="color:red;">Your number is negative</label>
你说得对 你的号码是负数
您可以这样做

HTML

CSS

.negative {
   color: red;
}
.positive {
    color: green;
}

我可以使用ng类而不是ng样式直接分配一个类吗?嗨,John,这很有效。如果我有两门课,那该怎么办。一类为正值,一类为负值。我怎样才能达到这种情况?帕特里克,我已经编辑了我的答案,以说明积极的价值观。您所需要做的就是向ng类添加一个新条件,并创建一个额外的类以在CSS中反映该条件。