Javascript 此应用程序中的视图中未显示控制器属性
下面是我的AngularJS应用程序中的Javascript 此应用程序中的视图中未显示控制器属性,javascript,html,angularjs,Javascript,Html,Angularjs,下面是我的AngularJS应用程序中的aap.js var app = angular.module('gallery',[]); (function(){ app.controller('GalleryController',function(){ this.tab = true; }); })(); 和gallery.html是: <html ng-app="gallery"> <head> <link
aap.js
var app = angular.module('gallery',[]);
(function(){
app.controller('GalleryController',function(){
this.tab = true;
});
})();
和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}}
g.tab
,这是控制器的属性,未显示在视图中。为什么会这样?再次编辑:我错过了阅读您的文章。您使用的是this
关键字correct,但是当您单击图像时,tab
属性没有显示,因为ng click
正在使用tab
而不是g.tab
。请参见更新的小提琴:
您不能在控制器中像那样使用this
关键字。控制器方法的上下文不是您的作用域。是的,你可以看到编辑
您可以在服务中使用该语法,但不能在控制器中使用,相反,您的范围将与其他服务、工厂等一起注入
应该是这样
app.controller('GalleryController',function($scope){
$scope.tab = true;
});
最有可能是杀伤力过大,但还是增加了小提琴来演示:
编辑:这可以通过使用This
关键字来实现。我不知道这个,每天都要学习新的东西。这是通过在ng控制器中使用as
操作符来实现的
请参阅更新的小提琴:
我在以下文件中找到它:
我的理解是,as
操作符将你的作用域绑定到你视图中的一个属性,你通过了testas myScope
意味着绑定你视图中test
属性的这个关键字
希望这是有意义的。再次编辑:我错过了阅读您的文章。您使用的是this
关键字correct,但是当您单击图像时,tab
属性没有显示,因为ng click
正在使用tab
而不是g.tab
。请参见更新的小提琴:
您不能在控制器中像那样使用this
关键字。控制器方法的上下文不是您的作用域。是的,你可以看到编辑
您可以在服务中使用该语法,但不能在控制器中使用,相反,您的范围将与其他服务、工厂等一起注入
应该是这样
app.controller('GalleryController',function($scope){
$scope.tab = true;
});
最有可能是杀伤力过大,但还是增加了小提琴来演示:
编辑:这可以通过使用This
关键字来实现。我不知道这个,每天都要学习新的东西。这是通过在ng控制器中使用as
操作符来实现的
请参阅更新的小提琴:
我在以下文件中找到它:
我的理解是,as
操作符将你的作用域绑定到你视图中的一个属性,你通过了testas myScope
意味着绑定你视图中test
属性的这个关键字
希望这是有意义的。再次编辑:我错过了阅读您的文章。您使用的是this
关键字correct,但是当您单击图像时,tab
属性没有显示,因为ng click
正在使用tab
而不是g.tab
。请参见更新的小提琴:
您不能在控制器中像那样使用this
关键字。控制器方法的上下文不是您的作用域。是的,你可以看到编辑
您可以在服务中使用该语法,但不能在控制器中使用,相反,您的范围将与其他服务、工厂等一起注入
应该是这样
app.controller('GalleryController',function($scope){
$scope.tab = true;
});
最有可能是杀伤力过大,但还是增加了小提琴来演示:
编辑:这可以通过使用This
关键字来实现。我不知道这个,每天都要学习新的东西。这是通过在ng控制器中使用as
操作符来实现的
请参阅更新的小提琴:
我在以下文件中找到它:
我的理解是,as
操作符将你的作用域绑定到你视图中的一个属性,你通过了testas myScope
意味着绑定你视图中test
属性的这个关键字
希望这是有意义的。再次编辑:我错过了阅读您的文章。您使用的是this
关键字correct,但是当您单击图像时,tab
属性没有显示,因为ng click
正在使用tab
而不是g.tab
。请参见更新的小提琴:
您不能在控制器中像那样使用this
关键字。控制器方法的上下文不是您的作用域。是的,你可以看到编辑
您可以在服务中使用该语法,但不能在控制器中使用,相反,您的范围将与其他服务、工厂等一起注入
应该是这样
app.controller('GalleryController',function($scope){
$scope.tab = true;
});
最有可能是杀伤力过大,但还是增加了小提琴来演示:
编辑:这可以通过使用This
关键字来实现。我不知道这个,每天都要学习新的东西。这是通过在ng控制器中使用as
操作符来实现的
请参阅更新的小提琴:
我在以下文件中找到它:
我的理解是,as
操作符将你的作用域绑定到你视图中的一个属性,你通过了testas myScope
意味着绑定你视图中test
属性的这个关键字
希望这是有意义的。将所有值绑定到$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>
{{tab}}
将所有值绑定到$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>
{{tab}}
将所有值绑定到$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>
{{tab}}
将所有值绑定到$scope