2017-12-20 29 views
0

のは、私は関数式外部JSファイルをAngularに含める方法とAngularで呼び出す方法

$scope.submit = function() { 
    if ($scope.username && $scope.password) { 
    var user = $scope.username; 
    var pass = $scope.password; 
    if (pass == "admin" && user == "[email protected]") { 
    alert("Login Successful"); 
    jQuery(location).attr('href', 'http://google.com') 
    } else if (user != "[email protected]") { 
    alert("Invalid username"); 
    } else if (pass != "admin" && user == "[email protected]") { 
    alert("Invalid password"); 
    } 
} 
}. 

を持ってindex.jsという名前のファイルがあるとしましょう、私は私の角のプロジェクトで、その関数を呼び出したいです。私はどうしたらいいですか?

答えて

0

この関数をindex.jsファイルからエクスポートして、必要なファイルにインポートするだけです。 window.myFunction = myFunctionのようなウィンドウオブジェクトにその関数をアタッチし、htmlにindex.jsをスクリプトタグとして追加します。

2

AngularJS関数式をAngularで使用しようとしているようですが、これは正しくありません。

$scopeのようなものはありません。

機能を変更しても問題ない場合は、次のような操作を行います。 index.js

export function submit(username, password) { 
    if (username && password) { 
    var user = username; 
    var pass = password; 
    if (pass == "admin" && user == "[email protected]") { 
     alert("Login Successful"); 
     jQuery(location).attr('href', 'http://google.com') 
    } else if (user != "[email protected]") { 
     alert("Invalid username"); 
    } else if (pass != "admin" && user == "[email protected]") { 
     alert("Invalid password"); 
    } 
    } 
} 

そして、あなたはそれを使用するファイル内で

import submit from index.js 

submit('usr', 'pwd'); 
+0

@Asish anglecliにjsファイルのパスを含める必要がありますか? – Manoj

+0

必要ありません。 angular-cliが 'import submit index.js'ステートメントに遭遇すると、それは自動的にビルドされます。 – Ashish

+0

こんにちは、@Ashishあなたが言ったように私はすべてを試みました。送信ボタンをクリックしている間は、無効なユーザーしか表示されません。そのページが無効であることを表示しても、私を助けてくれますか? – Manoj

関連する問題