angular组件中的模板没有按预期工作
Template in angular component not working as expected.
这是我的index.html文件。
<!doctype html>
<html lang="en" ng-app="phonecatApp">
<head>
<meta charset="utf-8">
<title>Google Phone Gallery</title>
<link rel="stylesheet" href="bower_components/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="app.css" />
<script src="bower_components/angular/angular.js"></script>
<script src="app.js"></script>
<script src="phone-list.component.js"></script>
</head>
<body>
<!-- Use a custom component to render a list of phones -->
<phone-list></phone-list>
</body>
</html>
这是我的phone-list.component.js文件
// Register 'phoneList' component, along with it's associated controller and template'
angular.
module('phonecatApp').
component('phoneList', {
template:
'<ul>' +
'<li ng-repeat="phone in $ctrl.phones">' +
'<span>{{phone.name}}</span>' +
'<p>{{phone.snippet}}</p>' +
'</li>' +
'</ul>' + '<h3>To be honest, my real name is {{me.name}}</h3>',
controller: function phoneListController() {
this.phones = [
{
name:'Nexus 5',
snippet: 'Fast just got faster with Nexus 5'
},
{
name: 'Motorolla Xoom with Wi-Fi',
snippet: 'The Next, Next Generation Tablet'
},
{
name: 'Motorolla Xoom',
snippet: 'The Next, Next Generation Tablet'
}
];
this.me = [
{
name: 'shameem',
age: 18
}
]
}
});
me.name应该显示在index.html页面的phone-list组件中。我不明白为什么我的名字没有显示在页面上。
请使用以下代码
'</ul>' + '<h3>To be honest, my real name is {{$ctrl.me[0].name}}</h3>',
代替
'</ul>' + '<h3>To be honest, my real name is {{$ctrl.me.name}}</h3>',
这个问题是因为你正在使用数组对象并试图直接访问它。或者你可以使用ng-repeat
Or Else Make
this.me = {name: 'shameem', age: 18}
代替
this.me = [{name: 'shameem', age: 18}]
和可以使用相同的HTML,如果你改变"this。
相关文章:
- 我的shareService在angular 2中发送值工作正常,但当我渲染我的另一个组件时,会获得默认值
- 如何让 JavaScript 在 React 组件中工作
- react如何与修改html的javascript ui组件协同工作
- 将数据传回父组件的ReactJS组件无法工作
- React组件onClick处理程序不工作
- Redux - 繁重的工作应该在哪里发生 - 化简器,动作,容器或表示组件
- 如何让两个不相关的 React 组件一起工作
- 是否有一个JavaScript / Jquery组件可以创建一个像Eclipse一样的工作空间
- 了解嵌套组件绑定在 KnockoutJS 上的工作原理
- 无法让 port.emit 和 port.on 在 Firefox 附加组件中工作
- UIKit (getuikit) 滑块组件拒绝工作
- 为什么我的应用程序中的某些组件在IE7中工作,而在IE9中不起作用
- 正在更新不工作的组件ReactJs
- 附加组件生成器:使用端口的多个工作程序
- AngularJS应用程序组件(模块、工厂、控制器)定义的确切工作方式
- 自定义组件ExtJS,而不是工作setValue
- 我如何让我的自定义UI组件与我的自定义Wordpress主题正确工作
- 切换组件不能在跨包指令上工作
- angular组件中的模板没有按预期工作
- React组件拖动不工作,没有错误或控制台日志