2014-01-08 38 views
0

我是一個新手使用指令的角度。有一個問題我需要幫助。 我有一個文件common.js。該文件內容的方法:如何調用指令外的方法?

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

而且在指令:

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. 
         //.... 
        } 
       }); 
      } 
     } 
    }); 

有人幫幫我嗎?

回答

2

如果您已經加載了common.js腳本,您可以像平常一樣調用該函數。這似乎是全球..

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

可以加載腳本正常在HTML:<script src="path/to/common.js"></script>

而且,我希望這只是一個例子。它不會使大量的感覺自己包裝alert()。您可以直接撥打alert(),不需要外部功能。

+0

是的。有用。我錯了,因爲rem腳本在html中 vodanh

相關問題