Question

I am a newbie use directive in angular. There is a issue I need help. I have a file common.js . This file content method :

function showAlert(value) {
   alert(value);
}

And in directive :

app.directive('ccDecimalinput', function($timeout, $parse){
      var FOCUS_CLASS = "error_tip error";
      var templateOut = '';
    //  console.log('ccDecimalinput ... ');
      return {
        restrict: 'E',
        require: 'ngModel',
        scope : {
            ngModel: '='
        },
        template: '<div ng-form="signup_form"><input type="text" class="maxlength_10_text left_aligned" id="' + attrs.id + '" name="' + attrs.name + '" ng-model="ngModel" required ng-minlength="1" ng-maxlength="10" /></div>'; ,
        replace : true,
        link: function(scope, ele, attrs, c) {
              scope.$watch('ngModel', function() {
                  if (scope.signup_form.$dirty && scope.signup_form.$invalid) {
                      //TODO
                      //I want to use method showAlert in common.js file here.
                      //....
                  }
              });
            }
        }
    });

Anybody help me ?

Was it helpful?

Solution

If you have loaded the common.js script, you can just call the function like you would normally. It appears to be global..

if (scope.signup_form.$dirty && scope.signup_form.$invalid) {
  showAlert('some value');
}

You can load your script as normal in your html: <script src="path/to/common.js"></script>

Also, I'm hoping that's just an example.. It doesn't make a lot of sense to wrap alert() by itself. You could just call alert() directly and not need the external function.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top