Issue
Hi everybody I am trying to follow a video lesson of Angular.js I added ng-app at the html tag, the ng-controller in the body to use the controller but I see all the {{elements}}. (If I remove the ng-controller, the input field works fine, but when I try to add the ng-controller, starts the problems... in the following code I should show only a list of authors
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>AngularJS</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="lib/js/app.js"></script>
</head>
<body class="bg-secondary" ng-controller="MyController">
<div class="container mt-sm-3">
<div class="row justify-content-center">
<div class="col-12 col-md-8 col-lg-6 card">
<div class="card-body">
<h2 class="card-title text-dark">Directory degli Artisti</h2>
<div class="form-row mb-2">
<div class="col">
<label class="col-form-label text-right" for="searchQuery">search {{' for: ' + query}}</label>
</div><!-- label -->
<div class="col-12">
<input class="form-control form-control-lg" id="searchQuery" placeholder="Search for artists" autofocus ng-model="query">
</div><!-- col-12 -->
</div><!-- form-row -->
</div><!-- card-body -->
</div><!-- col-12 -->
</div><!-- row -->
</div><!-- col-container -->
<div class="artist-list container">
<div class="row justify-content-center">
<div class="col-12 col-sm-9 col-md-7 col-lg-5">
<ul class=" list-group d-flex">
<li class="list-group-item">
<div class="media d-flex align-items-center">
<img class="rounded-circle mr-3"
src="images/{{artist.shortname}}_tn.jpg"
alt="Photo of {{artist.name}}">
<div class="media-body">
<h5 class="my-0 text-dark">{{artist.name}} </h5>
<div class="text-secondary font-italic">{{artist.study}}</div>
</div><!-- media-body -->
</div><!-- media -->
</li>
</ul>
</div><!-- col-12 -->
</div><!-- row -->
</div><!-- col-container -->
</body>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</html>
The file app.js, contains the list of author (only one in my example) is the following:
var myApp = angular.module('MyApp', []); // no dipendenze []
myApp.controller('MyController', function MyController($scope) {
$scope.artist = {
"name":"Emilio Alfieri",
"shortname":"Draco",
"study":"Ing. Informatica",
"bio":"Nato a Roma il 7/05/1987, si è sposato il 19 Agosto 2020 in Indonesia.Adora la chitarra e i computers."
}
}); //$scope è come una variabile globale
I receive the following error: Uncaught Error: [$injector:modulerr] I really followed all the instruction I have seen in the video. What's my mistake?
Thank you very much!
-
-
Solution
You made a spelling mistake of module name.
Replace var myApp = angular.module('myApp', []);
instead of
var myApp = angular.module('MyApp', []);
var myApp = angular.module('myApp', []);
myApp.controller('MyController', function($scope) {
$scope.artist = {
"name":"Emilio Alfieri",
"shortname":"Draco",
"study":"Ing. Informatica",
"bio":"Nato a Roma il 7/05/1987, si è sposato il 19 Agosto 2020 in Indonesia.Adora la chitarra e i computers."
}
});
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<title>AngularJS</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
</head>
<body class="bg-secondary" ng-controller="MyController">
<div class="container mt-sm-3">
<div class="row justify-content-center">
<div class="col-12 col-md-8 col-lg-6 card">
<div class="card-body">
<h2 class="card-title text-dark">Directory degli Artisti</h2>
<div class="form-row mb-2">
<div class="col">
<label class="col-form-label text-right" for="searchQuery">search {{' for: ' + query}}</label>
</div><!-- label -->
<div class="col-12">
<input class="form-control form-control-lg" id="searchQuery" placeholder="Search for artists" autofocus ng-model="query">
</div><!-- col-12 -->
</div><!-- form-row -->
</div><!-- card-body -->
</div><!-- col-12 -->
</div><!-- row -->
</div><!-- col-container -->
<div class="artist-list container">
<div class="row justify-content-center">
<div class="col-12 col-sm-9 col-md-7 col-lg-5">
<ul class=" list-group d-flex">
<li class="list-group-item">
<div class="media d-flex align-items-center">
<img class="rounded-circle mr-3"
src="images/{{artist.shortname}}_tn.jpg"
alt="Photo of {{artist.name}}">
<div class="media-body">
<h5 class="my-0 text-dark">{{artist.name}} </h5>
<div class="text-secondary font-italic">{{artist.study}}</div>
</div><!-- media-body -->
</div><!-- media -->
</li>
</ul>
</div><!-- col-12 -->
</div><!-- row -->
</div><!-- col-container -->
</body>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.7.9/angular.min.js"></script>
<script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
</html>
Answered By - M A Salman
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.