angularJs ng-model 中的函数调用?
Function calling inside angularJs ng-model?
我可以像下面这样在 angularJs 的 ng-model 中调用函数吗?
<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-modle 中调用函数,最好的方法是在 controller.For 示例
中更新它
<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
来定义函数。
我可以像下面这样在 angularJs 的 ng-model 中调用函数吗?
<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-modle 中调用函数,最好的方法是在 controller.For 示例
中更新它<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
来定义函数。