在这个Angular js应用中,控制器属性没有显示在视图中
Controller property not being show in view in this Angular js app
下面是我的AngularJS应用程序的aap.js
。
var app = angular.module('gallery',[]);
(function(){
app.controller('GalleryController',function(){
this.tab = true;
});
})();
and gallery.html
=
<html ng-app="gallery">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="app.js"></script>
<link rel="shortcut icon" href="">
</head>
<body ng-contoller="GalleryController as g">
<section >
<ul>
<li><img ng-click="tab=1" src="images/gem-01.jpg" height="100" /></li>
</ul>
<h1>{{g.tab}}</h1>
</section>
</body>
</html>
g.tab
是控制器的属性,没有显示在视图中。为什么会这样呢?
再次编辑:我错过了你的问题。您正在使用this
关键字正确,但是当您单击图像时,tab
属性未显示,因为ng-click
使用tab
而不是g.tab
。参见更新更新更新提琴:http://jsfiddle.net/z8uby8oz/2/
您不能在控制器中像这样使用可以看到编辑this
关键字。控制器方法的上下文不是你的作用域。
你可以在服务中使用这种语法,但不能在控制器中使用,相反,你的作用域是与其他服务、工厂等一起注入的。
应该是
app.controller('GalleryController',function($scope){
$scope.tab = true;
});
最可能是过度使用,但无论如何都添加了演示:http://jsfiddle.net/z8uby8oz/
编辑:这可以使用this
关键字来实现。我不知道这一点,每天学习新的东西。这是通过在ng-controller
中使用as
运算符实现的。
参见更新的小提琴:http://jsfiddle.net/z8uby8oz/1/和文档我发现它在:https://docs.angularjs.org/api/ng/directive/ngController#example
我的理解是as
运算符将您的作用域绑定到视图中的属性,您通过test as myScope
意味着将test
控制器的this
关键字绑定到视图中的myScope
属性。
希望你明白。
将所有值绑定到$scope
var app = angular.module('gallery',[]);
(function(){
app.controller('GalleryController',function($scope){
$scope.tab = true;
});
})();
你的HTML看起来像
<html ng-app="gallery">
<head>
<link rel="stylesheet" type="text/css" href="bootstrap.min.css">
<script type="text/javascript" src="angular.js"></script>
<script type="text/javascript" src="app.js"></script>
<link rel="shortcut icon" href="">
</head>
<body ng-contoller="GalleryController">
<section >
<ul>
<li><img ng-click="tab=1" src="images/gem-01.jpg" height="100" /></li>
</ul>
<h1>{{tab}}</h1>
</section>
</body>
</html>
相关文章:
- 带有填充属性的SVG矩形显示在包含元素的上方插入框阴影
- 重写CSS:使用jquery显示none属性
- 对象的属性显示为未定义
- 离子模态未打开(类型错误:无法读取未定义的属性“显示”)
- HTML “标题”属性显示单词“未定义” - 例如.“主页未定义”
- 使剑道 UI 下拉列表将“选择控件”的“标题”属性显示为剑道工具提示
- 内容显示中的 JavaScript 错误“无法设置未定义的属性”显示”
- 动态添加的控件属性显示为 controls=“ ” 而不是控件
- 为什么获胜't我的json对象的`email`属性显示在我的jade模板中
- Angular:如何根据项目的属性显示icon.png
- 如何使用属性显示带有CSS3转换的隐藏元素
- 文本和输入元素以相同的方式追加和给定属性.显示文本,但不显示输入.为什么
- 基于属性显示html
- 修改title属性显示的工具提示
- 使用标签选择图像文件,但有属性显示输入
- 使用使用rel属性显示的模式编辑gridview单元格
- 按数据属性显示列表中的前三项
- 使用数据弹出属性显示 Jquery-ui 对话框
- 使用数据弹出属性显示 UI 对话框
- 检测属性显示从无显示到显示块