我刚刚开始学习 Angular JS,但很快我就遇到了一个我不知道如何解决的问题。
此代码打印:
<h1>{{author[0].name}}</h1>
<p>{{author[0].title+ ', '+author[0].company}}
而不是:
<h1>MKJ</h1>
<p>Web Developer, Student Organization</p>
这里也给出了代码:
<!doctype html>
<!-- Declaring the ng-app -->
<html ng-app="myApp">
<head>
<title>Parking</title>
<!-- Importing the angular.js script -->
<script src="lib/angular/angular.min.js"></script>
<script>
var myApp = angular.module("myApp", []);
myApp.controller("MyController", function ($scope){
$scope.author = {
'author': 'MKJ',
'title': 'Web Developer',
'company': 'Student Organization',
};
}
</script>
</head>
<!-- Attaching the view to the MyController -->
<body ng-controller="MyController">
<h1>{{author[0].name}}</h1>
<p>{{author[0].title+ ', '+author[0].company}}
查看此代码片段或更正 JS fiddle 上的代码?
var myApp = angular.module("myApp", []);
myApp.controller("MyController", function ($scope){
$scope.author = {
'author': 'Ravy VIllalbobs',
'title': 'Staff Author',
'company': 'Lynda.com',
};
}
<div ng-controller="MyController">
<h1>{{author.name}}</h1>
<p>{{author.title+ ', '+author.company}}
</div>
最佳答案
$scope.author
是一个对象,因此您不需要在此处的括号中指定索引
<h1>{{author.name}}</h1>
<p>{{author.title+ ', '+author.company}}
关于javascript - 当我使用 Controller 和模块时,Angular JS 表达式无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893566/