2014-10-30 41 views
0

如何從角度控制器調用showNotification() JS函數?這是因爲我正在使用一些只包含該JS文件中的代碼的插件。如何從Angular控制器調用JavaScript函數?

authentication.controller('loginController', function($scope , $http) { 
$http({ 
      method: 'POST', 

      headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, 
      data: $.param(($scope.loginData)), 

     }).success(function(data, status, headers, config) 
      { 
       if(data.status == "success"){ 
      } 
      else 
      { 
       alert("invalid credentials"); 
       showNotification(){ 
         message: "This is Auto Close notification. Message will close after 2 seconds", 
         autoClose: true, 
         duration: 2 
        } //THIS IS JS FUNCTION 
      } 
      }). 
      error(function(data, status, headers, config) { 

       console.log("Login failed"); 
       showNotification(){ 
         message: "This is Auto Close notification. Message will close after 2 seconds", 
         autoClose: true, 
         duration: 2 
        } //THIS IS JS FUNCTION 
       }); 
}); 

我在不同的JS文件JS功能

function showNotification(params){ 
    // Function Logic 
} 
+0

您是否在角度文件之前附加了外部js文件? – 2014-10-30 08:47:26

+0

yes ofcourse。我已經包含那個包含示例()函數 – 2014-10-30 08:49:24

+0

的JS文件,我不明白你的函數的調用!你把參數在身體看看http://stackoverflow.com/questions/26628619/angular-js-dependency-injection-in-global-function – Whisher 2014-10-30 09:07:35

回答

0

就包括你的插件的JavaScript文件,它應該工作。

要做到這一點正確的方式,最好是將你的插件包裝在一些angularJS服務或指令中。

+0

請解釋我。我已經添加了我的例子,但我得到錯誤 – 2014-10-30 08:23:52

+0

你得到了什麼錯誤? – tiledcode 2014-10-30 08:26:20

0

請參閱該演示http://plnkr.co/edit/unnI3qFBv2zah68MnTFj?p=preview

你一定要確保你的js函數文件的文件之前,控制器加載

var app = angular.module('app', []); 

app.controller('MainCtrl', function($scope) { 
    $scope.name = 'World'; 

    $scope.show = function() { 

    showNotification("Hello from Angular") 

    } 
}); 

....其他js文件的功能:

function showNotification(message){ 

    alert(message) 

} 
相關問題