Angular中,函数返回true,但是ng-show不工作
Angular, function return true but ng-show won't work
showOrHide函数将返回true如果我点击链接。
我用alert和console.log函数测试了这段代码,它可以工作,但是如果我想把这段代码测试成ng-show或ng-hide指令,它就不能工作了。我不明白这个问题。
Sum:函数返回false,如果我点击某个链接,它返回true但是true和false在ng-show中不起作用。
angular.js文件
$scope.showOrHide = function (value) {
if (typeof value !== 'undefined')
{
return alert(true);
//console.log("true");
//return true;
}else {
return alert(false);
//console.log("false");
//return false;
}
};
$scope.projectId = $stateParams.projectId;
$scope.showOrHide($stateParams.projectId);
html文件
<tr ng-repeat="n in data.projects track by $index"
ng-href="@{{n.id}}"
ui-sref="goTo({ projectId: n.id })">
<td>@{{n.project_code}}</td>
<td>@{{n.project_name}}</td>
<td class="text-primary">will add</td>
<td class="text-primary">will add</td>
<td class="text-primary">@{{n.status}}</td>
<td ng-show="showOrHide">test</td>
</tr>
你的ng-show
是基于一个函数而不是一个变量,所以你需要把它写成一个函数。
<td ng-show="showOrHide('undefined')">test</td>
JS:
$scope.showOrHide = function (value) {
if (typeof value !== 'undefined')
return true;
else
return false;
};
如果我理解了您想要实现的目的(仅当$stateParams。投影存在)你可以只使用你已经设置的变量,所以删除函数,只使用下面的:
控制器:
app.controller('myController', function( $scope, $stateParams) {
// other code here...
$scope.projectId = $stateParams.projectId || false;
});
视图:
<tr ng-repeat="n in data.projects track by $index"
ng-href="@{{n.id}}"
ui-sref="goTo({ projectId: n.id })">
<td>@{{n.project_code}}</td>
<td>@{{n.project_name}}</td>
<td class="text-primary">will add</td>
<td class="text-primary">will add</td>
<td class="text-primary">@{{n.status}}</td>
<td ng-show="projectId">test</td>
</tr>
我放弃了所有的解决方案,因为这些都不起作用,所以我没有修改我的代码。我都改了
这就是解决方案。
JS文件
$scope.checkThis = true;
$scope.isShow = function (number) {
return number;
};
$scope.toFalse = function () {
$scope.checkThis = false;
};
$scope.toTrue = function () {
$scope.checkThis = true;
};
HTML文件
<tr ng-repeat="n in data.projects track by $index"
ng-href="@{{n.id}}"
ui-sref="goTo({ projectId: n.id })">
<td>@{{n.project_code}}</td>
<td>@{{n.project_name}}</td>
<td class="text-primary">will add</td>
<td class="text-primary">will add</td>
<td class="text-primary">@{{n.status}}</td>
<td class="text-primary" ng-click="toFalse()"
ng-show="isShow(checkThis)">test</td>
</tr>
我认为这是简单的方法。我浪费了3个小时。
创建如下结果变量:
$scope.toShowOrToHide = $scope.showOrHide($stateParams.projectId);
HTML: 和
<td ng-show="toShowOrToHide">test</td>
和JS返回没有警告:
$scope.showOrHide = function (value) {
return (typeof value !== 'undefined');
};
编辑:
$scope.toShowOrToHide = function() {
return $scope.showOrHide($stateParams.projectId);
);
HTML: 和
<td ng-show="toShowOrToHide()">test</td>
你可能已经决定了什么最适合你....
First
$scope.stateParams = $stateParams;
然后ng-show="stateParams.projectId"
不需要函数,参数改变时值自动更新
相关文章:
- 使用angularjs ng Show/ng hide显示和隐藏不同的内容
- 我需要NG-IF/NG-SWITCH还是NG-SHOW&NG-HIDE
- 如何在AngularJS视图中使用promise(ng-show)
- AngularJS:如何使用java脚本函数更改跨度的ng-show条件
- ng show和ng hide don'I don’我一秒钟都不工作
- AngularJS:使用ng-show/hide动态维护下拉列表的3种状态(InProgress、Success、Err
- ng-show内容在页面加载过程中闪烁,尽管它不是真实的,并且ng-cloak不适用于FF
- 如何验证使用 ng-repeat、ng-show(角度)动态创建的输入
- 如何使用ng-repeat与清单模型进行ng-show
- AngularJS:ng-show不适用于ng-app
- 为什么我的html节点保持类ng隐藏属性ng show=true
- AngularJS:为什么ng show没有在IE11上运行
- Angularjs ng-show not working with checkbox with default val
- Angularjs - grouping ng show
- AngularJS:隐藏<td>使用ng-hide/ng-show没有间隙
- Angularjs ng-show == Id?
- 基于参数(angular)调用特定元素上的ng show
- 只有当我导航回窗口时,angular ng show才有效
- 使ng-show/ng-hide与由Angular之外的外部源修改的localStorage一起工作
- Angular中,函数返回true,但是ng-show不工作