Search code examples
angularjsumbracoumbraco7

AngularJs doesn't call controller with umbraco plugin


I'm new to AngularJS. I'm trying to do a Umbraco Plugin for character limit in the textarea but I'm having a problem calling a function in the controller. My controller is

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';}
        }

    }); 

and I'm calling the controller from

<div ng-controller="Example.CharLimitController">
<textarea cols="10" ng-model="model.value" ng-change="limitchars"></textarea>
    <br/>
    <span ng-bind="info"></span>
</div>

When I'm loading the page Alert(0) is displayed but on change of the textarea Alert(1) is not diaplayed nor nothing below.

Please help


Solution

  • 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>