AngularJS表达式未求值
AngularJS expression not evaluating
我是AngularJS的初学者,目前正在学习基础知识。我试图创建一个简单的菜单,但我有几个Angular表达式没有进行评估;{{dish.description}}和{dish.comments}}。我试着在Chrome的开发者控制台上调试页面,但没有出现任何错误,所以我不确定问题出在哪里。
<!DOCTYPE html>
<html lang="en" ng-app="confusionApp">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head
content must come *after* these tags -->
<title>Ristorante Con Fusion: Menu</title>
<!-- Bootstrap -->
<link href="../bower_components/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
<link href="../bower_components/bootstrap/dist/css/bootstrap-theme.min.css" rel="stylesheet">
<link href="../bower_components/font-awesome/css/font-awesome.min.css" rel="stylesheet">
<link href="styles/bootstrap-social.css" rel="stylesheet">
<link href="styles/mystyles.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="row row-content" ng-controller="dishDetailController as dishCtrl">
<div class="media" ng-repeat="dishes in dishCtrl.dish">
<div class="col-xs-12">
<p> {{ dishes.description }} </p>
</div>
<div class="col-xs-9 col-xs-offset-1">
<p> {{ dishes.comments }} </p>
</div>
</div>
</div>
</div>
<script src="../bower_components/angular/angular.min.js"></script>
<script>
var app = angular.module('confusionApp',[]);
app.controller('dishDetailController', function() {
var dish={
name:'Uthapizza',
image: 'images/uthapizza.png',
category: 'mains',
label:'Hot',
price:'4.99',
description:'A unique combination of Indian Uthappam (pancake) and Italian pizza, topped with Cerignola olives, ripe vine cherry tomatoes, Vidalia onion, Guntur chillies and Buffalo Paneer.',
comments: [
{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:4,
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
date:"2014-09-05T17:57:28.556094Z"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}
]
};
this.dish = dish;
});
</script>
</body>
</html>
dish
应该是一个数组,如果您想对它执行ng-repeat
。
var dish=[{...object1..}, {..object2... ]};
- 首先,您需要将dish指定为$scope变量-$scope.dishAngular提供了允许控制器变量绑定的范围,并且可以在HTML中访问
-
ng重复使用来迭代DOM中的$scope变量,因此frthis而不是dish={name:'Uthapizza',…},它必须类似dish=[object1,object2];例如
$scope.dish=[{description:'description 2',comments:'comment 1'}, {description:'description 2',comments:'comment 2'}]
- 您可能会得到注入器错误,为此您必须在控制器中注入$scope
- 你不需要使用this.dish=dish在angularJS中。还附上了解决方案
var app = angular.module('confusionApp',[]);
app.controller('dishDetailController', function($scope) {
$scope.dish=[{
rating:5,
comment:"Imagine all the eatables, living in conFusion!",
author:"John Lemon",
date:"2012-10-16T17:57:28.556094Z"
},
{
rating:4,
comment:"Sends anyone to heaven, I wish I could get my mother-in-law to eat it!",
author:"Paul McVites",
date:"2014-09-05T17:57:28.556094Z"
},
{
rating:3,
comment:"Eat it, just eat it!",
author:"Michael Jaikishan",
date:"2015-02-13T17:57:28.556094Z"
},
{
rating:4,
comment:"Ultimate, Reaching for the stars!",
author:"Ringo Starry",
date:"2013-12-02T17:57:28.556094Z"
},
{
rating:2,
comment:"It's your birthday, we're gonna party!",
author:"25 Cent",
date:"2011-12-02T17:57:28.556094Z"
}
];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
<div class="container" ng-app="confusionApp">
<div class="row row-content" ng-controller="dishDetailController as dishCtrl">
<div class="media" ng-repeat="dishes in dish">
<div class="col-xs-12">
<p> {{ dishes.author }} </p>
</div>
<div class="col-xs-9 col-xs-offset-1">
<p> {{ dishes.comment }} </p>
</div>
<div class="col-xs-9 col-xs-offset-1">
<p> {{ dishes.rating }} </p>
</div>
</div>
</div>
</div>
相关文章:
- 我的AngularJS表达式没有'不起作用
- AngularJS:带有HTML和angular表达式的指令;编译”;具有外部范围的内容
- 组合angularjs表单字段的两个正则表达式
- AngularJS ngTranscluded、angular.noop和插值VS表达式
- 使用正则表达式angularjs验证文件路径
- 在输入中存储AngularJS表达式结果
- Javascript / AngularJS对象选择基于表达式作为键
- Angularjs 与 coffeescript 函数表达式中断
- AngularJS-在$event.stopPropagation之后停止ng-click表达式求值
- AngularJS观察指令属性表达式,动态继承作用域
- 为什么javascript替换方法(没有regex)在angularjs表达式中不起作用来删除 
- Angularjs表达式未显示在ngDialog模板中
- AngularJs针对多个条件评估表达式
- AngularJS ng显示表达式在加载时不起作用
- AngularJS表达式未求值
- 使用angularjs表达式复选框
- 如何从json中检索angularjs表达式的数据
- 如何根据一定的标准在angularjs中使用正则表达式添加密码验证
- 堆叠三元运算符在 Angularjs 表达式中不起作用
- 表达式angularjs中的组合属性