在Angular JS的控制器中添加一个声明的对象
add an object declared on the controller in Angular JS
我是一个angularJS初学者,我有一个问题。我想添加数据到一个媒体对象在html中使用在控制器内的脚本中声明的对象,但我找不到哪个指令我必须使用。
代码如下:
<!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="col-xs-12">
<div class="media">
<div class="media-left media-middle">
<a href="#">
<img class="media-object img-thumbnail" ng-src={{dish.image}} alt="Uthapizza">
</a>
</div>
<div class="media-body">
<h4 class="media-heading">{{dish.name}}<span class="label label-danger">{{dish.label}}</span><span class="badge">{{dish.price | currency}}</span></h4>
<p>{{dish.description}}</p>
</div>
</div>
</div>
<div class="col-xs-9 col-xs-offset-1">
<p>Put the comments here</p>
</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>
有人能帮帮我吗?我认为这很简单,但我知道我已经崩溃了!
谢谢
你至少有2个选择
选项1:使用$scope。这将确保你的html保持不变
app.controller('dishDetailController', function($scope) {
// Your dish declaration
$scope.dish = dish;
});
选项2:Javascript保持不变。在HTML中,因为你使用dishDetailController as dishCtrl
示例:用
代替<img class="media-object img-thumbnail" ng-src={{dish.image}} alt="Uthapizza">
<img class="media-object img-thumbnail" ng-src={{dishCtrl.dish.image}} alt="Uthapizza">
这不是完全清楚你之后,但我假设你只是想知道如何改变/更新控制器内的数据,并有它反映在视图内?
任何你添加到你的dish
对象,将在你的html中的dish属性下访问angularjs表达式。
的例子:
app.controller('dishDetailController', function() {
this.dish = {
colour : 'blue'
};
});
然后在view/html中:
<div ng-controller="dishDetailController">
{{dish.colour}}
</div>
如果你试图使用注释,因为这是我能看到的唯一你还没有实现的东西。
在你的html中,你可以这样做:<ul class="comments">
<li class="comment" ng-repeat="comment in dish.comments">
{{comment.comment}} - Author ({{comment.author}}) Rating : {{comment.rating}}
</li>
</ul>
相关文章:
- 一个'var'在函数内部声明
- 如何获得一个与用“”声明的变量工作方式相同的变量;设“;或在with块中
- 是否可以在另一个变量中使用未声明的变量
- AngularJS中多个指令的一个声明
- 为什么要将一个函数赋给一个变量,而不是声明一个命名函数
- 如何调用Crossrider中声明的javascript函数;background.js”;点击我网站页面上的一个按钮
- 为什么我的变量需要一个'var'声明,当它's已经在var声明中了
- 声明一个js变量,该变量将被我的2个jsp访问
- 你能在Javascript中创建一个对象而不声明每个值吗,以及如何
- 将一个简单的函数声明形成JavaScript中的闭包
- 无法使用typescript访问angular中另一个控制器中声明的$rootscope属性
- 我需要用javascript声明一个对象吗
- 用javascript声明一个简单函数
- 如何在一个地方声明angular ui引导程序日期选择器设置
- 用javascript在一个对象的两个键上声明两个数组的简洁方法是什么
- 如何使用一个声明生成多个常量
- 需要声明,但找到'"显示:无"'.跳过到下一个声明
- javascript,我可以在一个声明/行中完成这项操作吗
- 在Angular JS的控制器中添加一个声明的对象
- 如何在Javascript中创建一个声明变量的菜单?