我可以在下面的angularJs中的ng模型中调用函数吗?在angularJs ng模型中调用函数?
<input type="text" ng-model="choice.number = itemNumber()" class="form-control" readonly>
任何帮助表示赞赏?
我可以在下面的angularJs中的ng模型中调用函数吗?在angularJs ng模型中调用函数?
<input type="text" ng-model="choice.number = itemNumber()" class="form-control" readonly>
任何帮助表示赞赏?
尝试使用ng-init
<input type="text" ng-model="choice.number" ng-init="choice.number = itemNumber()" >
如果你不喜欢,你必须有可能得到错误:[ngModel:nonassign]。欲了解更多信息,您可以看到https://docs.angularjs.org/api/ng/directive/ngModel。如果你需要以ng-mode调用函数,最好的方法是在控制器中更新它。例如
<div class="result" ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="choice.number" class="form-control" readonly>
{{choice.number}}
</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
var hello = function(){
return 5;
}
$scope.choice = {number:hello()};
//console.log($scope.hello());
});
</script>
我希望你会觉得这有帮助。您也可以使用$scope.hello
而不是var hello
来定义该功能。
使用'ng-init'而不是调用函数 –
ng-change? https://docs.angularjs.org/api/ng/directive/ngChange – Groben
@HJz其实我正在从数据库中获取choice.number的值,所以我必须使用JS函数调用PHP函数并从DB获取值所以我这里我必须在这里使用函数 –