我是AngularJS的新手。 我正在尝试为文本区域中的字符限制做一个Umbraco插件,但是在控制器中调用函数时遇到了问题。我的控制器是
angular.module("umbraco").controller("Example.CharLimitController", function ($scope) {
alert("0");
$scope.limitchars = function () {
alert("1");
var limit = 30;
if ($scope.module.value.length > limit) {
$scope.info = 'You cannot write more than ' + (limit) + ' Characters ';
$scope.module.value = $scope.module.value.substr(0, limit);
}
else
{ $scope.info = 'You have ' + (limit - $scope.model.value.length) + ' Characters left';}
}
});
然后我从
调用控制器<div ng-controller="Example.CharLimitController">
<textarea cols="10" ng-model="model.value" ng-change="limitchars"></textarea>
<br/>
<span ng-bind="info"></span>
</div>
当我加载页面时,会显示Alert(0),但在文本区域更改时,不会显示Alert(1),也不会在下面显示任何内容。
请帮助
答案 0 :(得分:1)
ng更改的语法为:
ng-change =limitChars()
我在构造函数中看不到任何模型对象。
您可以将其更改为仅用于模型检查吗?
<div ng-controller="Example.CharLimitController">
<textarea cols="10" ng-model="model" ng-change="limitchars()"></textarea>
<br/>
<span ng-bind="info"></span>
</div>
答案 1 :(得分:1)
angular.module("umbraco", []).controller("Example.CharLimitController", ['$scope', function($scope) {
$scope.limitchars = function() {
var limit = 3;
if ($scope.model.value.length > limit) {
$scope.info = 'You cannot write more than ' + (limit) + ' Characters ';
$scope.model.value = $scope.model.value.substr(0, limit);
} else {
$scope.info = 'You have ' + (limit - $scope.model.value.length) + ' Characters left';
}
}
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.4/angular.min.js"></script>
<div ng-app="umbraco">
<div ng-controller="Example.CharLimitController"> <textarea cols="10" ng-model="model.value" ng-change="limitchars()"></textarea> <br/> <span ng-bind="info"></span> </div>
</div>