Javascript 用这个。在角度上不起作用?
我用angularjs创建了一个简单的按钮。因此,视图中的HTML代码如下所示:Javascript 用这个。在角度上不起作用?,javascript,html,angularjs,Javascript,Html,Angularjs,我用angularjs创建了一个简单的按钮。因此,视图中的HTML代码如下所示: <div ng-controller="ButtonCtrl"> <button ng-click="ButtonCtrl.setIndex(1)">Create another bidding query</button> <button ng-click="ButtonCtrl.setIndex(2)">Create another
<div ng-controller="ButtonCtrl">
<button ng-click="ButtonCtrl.setIndex(1)">Create another bidding query</button>
<button ng-click="ButtonCtrl.setIndex(2)">Create another asking query</button>
<form ng-hide="ButtonCtrl.isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
但是,我没有得到预期的行为。当我单击“创建另一个投标查询”按钮时,表单不会隐藏自己。当我使用$scope函数替换变量时,例如$scope.index=0;,这个程序有效
我认为问题不在于使用这个.index,因为它对我的其他程序很有效。那么,确切的问题是什么呢?假设您只想让示例代码正常工作,我注意到了一些事情。首先,大多数Angular代码使用
$scope
来代替它,以便您可以利用Angular提供的scope功能。实际上,$scope
是用来代替这个
。因此,按照角度的方式,控制器将如下所示:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
此外,一旦声明了控制器(在标记中),就不需要为每个方法调用添加前缀。这是正确使用$scope
的好处之一。因此,标记将更改为如下内容:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
创建另一个投标查询
创建另一个询问查询
填写投标表格
希望这有帮助。假设您只是想让示例代码正常工作,我注意到了一些事情。首先,大多数Angular代码使用
$scope
来代替它,以便您可以利用Angular提供的scope功能。实际上,$scope
是用来代替这个
。因此,按照角度的方式,控制器将如下所示:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
此外,一旦声明了控制器(在标记中),就不需要为每个方法调用添加前缀。这是正确使用$scope
的好处之一。因此,标记将更改为如下内容:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
创建另一个投标查询
创建另一个询问查询
填写投标表格
希望这有帮助。假设您只是想让示例代码正常工作,我注意到了一些事情。首先,大多数Angular代码使用
$scope
来代替它,以便您可以利用Angular提供的scope功能。实际上,$scope
是用来代替这个
。因此,按照角度的方式,控制器将如下所示:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
此外,一旦声明了控制器(在标记中),就不需要为每个方法调用添加前缀。这是正确使用$scope
的好处之一。因此,标记将更改为如下内容:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
创建另一个投标查询
创建另一个询问查询
填写投标表格
希望这有帮助。假设您只是想让示例代码正常工作,我注意到了一些事情。首先,大多数Angular代码使用
$scope
来代替它,以便您可以利用Angular提供的scope功能。实际上,$scope
是用来代替这个
。因此,按照角度的方式,控制器将如下所示:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
此外,一旦声明了控制器(在标记中),就不需要为每个方法调用添加前缀。这是正确使用$scope
的好处之一。因此,标记将更改为如下内容:
app.controller('ButtonCtrl', function($scope) {
$scope.index=0;
$scope.setIndex=function(setbutt){
$scope.index=setbutt;
};
$scope.isSelected=function(checkbutt){
return $scope.index===checkbutt;
};
});
<div ng-controller="ButtonCtrl">
<button ng-click="setIndex(1)">Create another bidding query</button>
<button ng-click="setIndex(2)">Create another asking query</button>
<form ng-hide="isSelected(1)">
<h4>Filling The Bidding Form</h4>
<fieldset class="form-group">
<textarea class="form-control" ></textarea>
</fieldset>
</form>
<div>
创建另一个投标查询
创建另一个询问查询
填写投标表格
希望这能有所帮助。您应该使用
$scope
或只使用scope中的变量,而不是此
像这样:
$scope.index = 0;
$scope.setIndex = function(i) {
$scope.index = i;
};
$scope.isSelected = function(i) {
return ($scope.index === i);
};
(或者,例如
var index=0;
,如果出于某种原因您不希望它在范围内)。您应该使用$scope
或者只使用范围内的变量,而不是此
像这样:
$scope.index = 0;
$scope.setIndex = function(i) {
$scope.index = i;
};
$scope.isSelected = function(i) {
return ($scope.index === i);
};
(或者,例如
var index=0;
,如果出于某种原因您不希望它在范围内)。您应该使用$scope
或者只使用范围内的变量,而不是此
像这样:
$scope.index = 0;
$scope.setIndex = function(i) {
$scope.index = i;
};
$scope.isSelected = function(i) {
return ($scope.index === i);
};
(或者,例如
var index=0;
,如果出于某种原因您不希望它在范围内)。您应该使用$scope
或者只使用范围内的变量,而不是此
像这样:
$scope.index = 0;
$scope.setIndex = function(i) {
$scope.index = i;
};
$scope.isSelected = function(i) {
return ($scope.index === i);
};
(或者,例如
var index=0;
,如果出于某种原因您不希望它在范围内)。如果您希望能够使用语法按钮trl.setIndex(1)
则必须将此
分配给控制器中的$scope
,如最后一行所示:
app.controller('ButtonCtrl', function($scope) {
this.index=0;
this.setIndex=function(setbutt){
this.index=setbutt;
};
this.isSelected=function(checkbutt){
return this.index===checkbutt;
};
// set ButtonCtrl to the $scope
$scope.ButtonCtrl = this;
});
下面是一个很好的egghead.io视频,它解释了这一点:
但正如其他答案已经指出的那样。这并不是AngularJS的惯用用法。如果您想使用语法
按钮trl.setIndex(1)
您必须将此
分配给控制器中的$scope
,如最后一行所示:
app.controller('ButtonCtrl', function($scope) {
this.index=0;
this.setIndex=function(setbutt){
this.index=setbutt;
};
this.isSelected=function(checkbutt){
return this.index===checkbutt;
};
// set ButtonCtrl to the $scope
$scope.ButtonCtrl = this;
});
下面是一个很好的egghead.io视频,它解释了这一点:
但正如其他答案已经指出的那样。这并不是AngularJS的惯用用法。如果您想使用语法
按钮trl.setIndex(1)
您必须将此
分配给控制器中的$scope
,如最后一行所示:
app.controller('ButtonCtrl', function($scope) {
this.index=0;
this.setIndex=function(setbutt){
this.index=setbutt;
};
this.isSelected=function(checkbutt){
return this.index===checkbutt;
};
// set ButtonCtrl to the $scope
$scope.ButtonCtrl = this;
});
下面是一个很好的egghead.io视频,它解释了这一点:
但正如其他答案已经指出的那样。这并不是AngularJS的惯用用法。如果您想使用语法
按钮trl.setIndex(1)
您必须将此
分配给控制器中的$scope
,如最后一行所示:
app.controller('ButtonCtrl', function($scope) {
this.index=0;
this.setIndex=function(setbutt){
this.index=setbutt;
};
this.isSelected=function(checkbutt){
return this.index===checkbutt;
};
// set ButtonCtrl to the $scope
$scope.ButtonCtrl = this;
});
下面是一个很好的egghead.io视频,它解释了