2017-09-20 3 views
0

index.html:Verwendung externer JS-Datei in AngularJS Projekt

<!DOCTYPE html> 
<html> 
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"> 

</script> 
<script src="./assets/namesController.js"></script> 

<body ng-app="myApp"> 

    <!-- Method call from external JS files --> 
    <br> 
    <div ng-controller="ryanContrler"> 
     My first name : <input type="text" ng-model="ryanFirstname"> 
     <br> My last name : <input type="text" ng-model="austinLastname">   
     <br> My full name : {{fullName()}} 
    </div> 

</body> 

</html> 

namesController.js:

angular.module('myApp').controller('ryanContrler', ['$scope', function ($scope{ 
    $scope.ryanFirstname = "Ryan", 
    $scope.austinLastname = "Austin", 
    $scope.fullName = function() { 
     return $scope.ryanFirstname + " " + $scope.austinLastname; 
    } 

}]); 

Hallo Leute,

Ich habe versucht, in eine externe JS-Datei zu verwenden, um meine AngularJS Project, aber egal was ich versuchte, es schien die JS-Datei nicht zu erkennen. Die Code Sampels sind kopiert, aber ich verstehe sie, trotzdem kann ich sie nicht zum Laufen bringen.

Ich habe die JS in den Ordner Assets eingefügt. Die HTML-Datei befindet sich im Ordner src.

Vielen Dank für Ihre Hilfe und Ideen!

Fehlercodes in Chrome: here new Errors

+3

Es wie unten ändern sollte, ist ein Tippfehler in Ihrem Controller. "Funktion" sollte "Funktion" sein. Könnten Sie das überprüfen? –

+0

können Sie Ihre Verzeichnisstruktur teilen – Rahul

+1

Haben Sie eine Nachricht von der Konsole? – TNU

Antwort

0

Ihr Controller

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

agentApp.controller('ryanContrler',function ($scope){ 
    $scope.ryanFirstname = "Ryan", 
     $scope.austinLastname = "Austin", 
     $scope.fullName = function() { 
      return $scope.ryanFirstname + " " + $scope.austinLastname; 
     } 

}); 

Ihre HTML

<!DOCTYPE html> 
<html ng-app="myApp"> 
<head> 
    <script type="text/javascript" src="angular/angular.js"></script> 

    <script src="assets/namesController.js"></script> 
</head> 


<body > 

<!-- Method call from external JS files --> 
<br> 
<div ng-controller="ryanContrler"> 
    My first name : <input type="text" ng-model="ryanFirstname"> 
    <br> My last name : <input type="text" ng-model="austinLastname"> 
    <br> My full name : {{fullName()}} 
</div> 

</body> 

</html> 
+0

leider dieser Tippfehler war nur hier beim Formatieren ... – felix9607

+0

Mit neuen bearbeiten versuchen Ich tat –

+0

Dann Fehlercode sagt: "404 ... namesController.js.js. Nicht gefunden" – felix9607

Verwandte Themen