2016-08-24 61 views
0

我想根據該字段中的值在輸入字段中更改字體顏色。 Ng風格不會「看到」範圍的值。因此顏色不會改變。ng樣式的範圍值

的代碼我使用:

<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color': {{saldo}} < 0.00 ? 'red' : 'green') }"> 

這是我得到的錯誤:

Error: [$parse:syntax] Syntax Error: Token '<' not a primary expression at column 12 of the expression [{'color': < 0.00 ? 'red' : 'green') }] starting at [< 0.00 ? 'red' : 'green') }]. 

所以,我怎麼能得到在NG-風格我'saldo的價值? (將{{saldo}}放在ng樣式之外,給出正確的值)

+0

我看你問了很多問題的無標記一個作爲值來改變字體顏色輸入字段中對。請在您的問題中標記正確的答案。我會幫助其他用戶及其Stackoverflow如何工作。 – lin

回答

1

您不需要{{}}ng-style

替換此:

<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color': {{saldo}} < 0.00 ? 'red' : 'green') }"> 

與此

<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color': saldo < 0.00 ? 'red' : 'green') }"> 
+0

Thx,沒有更多的錯誤。但結果是「綠色」。我認爲它是比較串saldo而不是價值? – data2info

+0

我不知道。在我看來,最好使用'ng-class',例如:'ng-class =「{'saldo-red':saldo <0.00,'saldo-green':saldo> ='0'}」'。但是正確的答案是@ nishi-bangar答案 – gianlucatursi

3

我建議這樣做,

<input disabled name="saldo" type="text" class="form-control" 
     placeholder="0.00" ng-model="saldo" 
     ng-style="saldo < 0.00 ? {'color':'red'} : {'color':'green'}"> 

希望這有助於..

乾杯

0

這被剪斷可能會幫助你。

var myApp = angular.module('myApp',[]); 
 

 
myApp.controller('GreetingController', ['$scope', function($scope) { 
 
    $scope.saldo = 1; 
 
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
<div ng-app="myApp"> 
 
    <div ng-controller="GreetingController"> 
 
<input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="saldo < 0.00 ? {color: 'red'} : {color: 'green'}"> 
 
    </div> 
 
</div>

0

請使用下面的代碼依賴於在該領域 <input disabled name="saldo" type="text" class="form-control" placeholder="0.00" ng-model="saldo" ng-style="{'color':saldo}">