此代码有效。我能够从model属性中显示并将新值绑定到模型的另一个属性。所以我认为当我提交数据绑定时,确保使用新值更新特定属性。
有没有办法通过AngularJS的数据绑定工具调用setter?现在我直接更新属性。
<div ng-controller="myAppController">
{{serviceLimit}}
{{serviceLimits.upperlimit}}
{{serviceLimits.getUpperLimit()}}
<input ng-model="serviceLimits.upperlimit">
</div>
<button type="submit" class="btn btn-success btn-lg btn-block">
<span class="glyphicon glyphicon-flash"></span> Submit!
</button>
angular.module('myApp',['ngRoute']) .config(function($routeProvider) {
$routeProvider.when('/', {
templateUrl: 'homeView.html',
controller: 'myAppController'
});
})
.controller('myAppController', function($scope) {
var serviceLimits = function(serviceSpec){
var thatserviceLimit = {};
thatserviceLimit.upperlimit = serviceSpec.upperlimit;
thatserviceLimit.getUpperLimit = function(){
return serviceSpec.upperlimit;
};
thatserviceLimit.getNewUpperLimit = function(){
return thatserviceLimit.upperlimit;
};
thatserviceLimit.setUpperLimit = function(limit){
console.log(limit);
serviceSpec.upperlimit = limit;
};
thatserviceLimit.getLowerLimit = function(){
return serviceSpec.lowerlimit;
};
return thatserviceLimit;
};
$scope.serviceLimits = serviceLimits({ 'upperlimit' : 10, 'lowerlimit' : 5 });
logserviceLimits = serviceLimits({ 'upperlimit' : 10, 'lowerlimit' : 5 });
console.log(logserviceLimits.getUpperLimit());
$scope.serviceLimit = 10;
});