Javascript 文本框中的角度表达式
我使用角度表达式作为文本框中的值来执行一些操作Javascript 文本框中的角度表达式,javascript,html,angularjs,Javascript,Html,Angularjs,我使用角度表达式作为文本框中的值来执行一些操作 `<tr> <td><input type="text" ng-model="product.costPrice" required placeholder=" product Cost" class="form- control" > <span
`<tr>
<td><input type="text" ng-model="product.costPrice" required
placeholder=" product Cost" class="form- control" > <span
class="help-block"></span></td>
</tr>
<tr>
<td><label>Profit</label></td></tr>
<tr>
<td><input type="text" ng-model="Product.profit" required
placeholder=" Profit in %" class="form-control"> <span
class="help-block"></span></td>
</tr>
<tr>
<td><label>price</label></td></tr>
<tr>
<td><input type="text" value="{{product.costPrice * Product.profit}}" ng-model="Product.sellingPrice" required
placeholder=" selling price" class="form-control"> <span
class="help-block"></span></td>
</tr>
<tr>
<td>
<p>Expense on Books : {{product.costPrice * Product.profit}} Rs</p>
</td>
</tr>`
`
利润
价格
帐面费用:{{product.costPrice*product.profit}}
`
表达式根本不工作,即使它在
标记中工作,我做错了什么???您可以创建一个函数(在函数内部,您可以编写并返回表达式):
并使用ng value
<input ng-value="myExpression()">
工作代码编写函数将帮助您进行计算,然后将结果提供给您的作用域。ng模型将为您绑定它。在这种情况下,ng值很容易避免
$scope.calculate = function(cost, profit) {
$scope.Product.sellingPrice = cost * profit;
}
这只会在文本框中显示结果
<input ng-model="Product.sellingPrice">
只需更换
value=“{{product.costPrice*product.profit}}”ng model=“product.sellingPrice”
与
ng model=“Product.sellingPrice=Product.costPrice*Product.price”
在以下行中:
<input type="text" value="{{product.costPrice * Product.profit}}" ng-model="Product.sellingPrice" required placeholder=" selling price" class="form-control">
工作代码:您可以在控制器中进行计算。。 或者试试这个
<tr>
<td><input type="text" ng-model="product.costPrice" required
placeholder=" product Cost" class="form- control" > <span
class="help-block"></span></td>
</tr>
<tr>
<td><label>Profit</label></td></tr>
<tr>
<td><input type="text" ng-model="Product.profit" required
placeholder=" Profit in %" class="form-control"> <span
class="help-block"></span></td>
</tr>
<tr>
<td><label>price</label></td></tr>
<tr>
<td><input type="text" ng-model="Product.sellingPrice" required
placeholder=" selling price" class="form-control"> <span
class="help-block"></span></td>
</tr>
<tr>
<td>
<p>Expense on Books : {{Product.sellingPrice = product.costPrice * Product.profit}} Rs</p>
</td>
</tr>
利润
价格
图书费用:{{Product.sellingPrice=Product.costPrice*Product.profit}
您提到了您的控制器并提供了控制器的代码。那么我如何访问控制器中的此文本框?通过访问Product.sellingPrice输入的ng模型仍然是“Product.sellingPrice”,我只是将“Product.costPrice*Product.price”的值评估为“Product.sellingPrice”通过在ng模型本身中编写它。我认为这种方法不可行,因为它的抛出错误向我展示了你的更新代码,我运行了代码,它工作得很好。你的回答非常方便,但我不理解为什么我的ng值无法填充值;下面是控制器代码angular.module('sbAdminApp').controller('smsCtrl',function($scope,$timeout,$http,$state){$scope.cancel=function(){$state.go('dashboard.home');}$scope.Product={};$scope.myExpression=function(){return$scope.Product.name+“hello”}您不应该同时给出ng模型和ng值。这没有意义。应该是这样的:
<tr>
<td><input type="text" ng-model="product.costPrice" required
placeholder=" product Cost" class="form- control" > <span
class="help-block"></span></td>
</tr>
<tr>
<td><label>Profit</label></td></tr>
<tr>
<td><input type="text" ng-model="Product.profit" required
placeholder=" Profit in %" class="form-control"> <span
class="help-block"></span></td>
</tr>
<tr>
<td><label>price</label></td></tr>
<tr>
<td><input type="text" ng-model="Product.sellingPrice" required
placeholder=" selling price" class="form-control"> <span
class="help-block"></span></td>
</tr>
<tr>
<td>
<p>Expense on Books : {{Product.sellingPrice = product.costPrice * Product.profit}} Rs</p>
</td>
</tr>