2017-12-20 28 views
0

의 내가 함수 표현식 내 코너 프로젝트에서 그 함수를 호출 할외부 JS 파일을 각도로 포함하는 방법과 각도로 호출하는 방법?

$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과 같은 창 개체에 해당 함수를 첨부하고 index.js를 HTML의 스크립트 태그로 추가하십시오.

2

각도가 정확하지 않은 AngularJS 기반 함수 표현식을 사용하려는 것 같습니다.

$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' 명령문을 만날 때 자동으로 그것을 build에 포함합니다. – Ashish

+0

안녕하세요, 저는 당신이 말한대로 모든 것을 시도했습니다. 제출 버튼을 클릭하는 동안 그것은 무효 한 사용자 만 모든 시간을 보여줍니다. 페이지가 잘못 표시 되어도 계속 표시 할 수 있습니까? – Manoj