javascript - 当我使用 Controller 和模块时,Angular JS 表达式无法工作

标签 javascript angularjs

我刚刚开始学习 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}}

updated fiddle

关于javascript - 当我使用 Controller 和模块时,Angular JS 表达式无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29893566/

相关文章:

javascript - Angular 中带有关键字标签的文本区域

javascript - 结合 AngularJS 和 Twitter Bootstrap 的最佳方式

javascript - 如何让用户禁用推送通知?

javascript - Document.write 不捕获值 - Angularjs 项目

javascript - js - 1小时后停止

javascript - 如何使实时 HTML 预览文本区域免受 HTML/脚本注入(inject)的影响

javascript - 如何将状态作为 Prop 传递以便您可以修改它们

Javascript获取json对象的最后一个值

angularjs - 如何调试 Angular for 循环

javascript - 在 JavaScript 中 init 期间调用函数的正确方法