我有一个带有toggleFilter按钮的UI-Grid:
$scope.gridOptions = {
...
enableFiltering: false,
...
}
$scope.toggleFilter = function () {
$scope.gridOptions.enableFiltering = !$scope.gridOptions.enableFiltering;
$scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
};
<div id="filter" ng-click="toggleFilter()">
({{$scope.gridOptions.enableFiltering ? "-" : "+"}}) {{$scope.gridOptions.enableFiltering ? "Dis" : "En"}}able Filtering
</div>
该切换是按预期工作的,并且文本在加载时看起来正确,但不会随enableFiltering
的更改而更新。正确绑定此属性的缺少是什么?
从您的HTML中删除$范围。
({{gridOptions.enableFiltering ? "-" : "+"}}) {{gridOptions.enableFiltering ? "Dis" : "En"}}able Filtering
在这里工作https://plnkr.co/edit/vt6ae8ajxrqonhq8uekq?p=preview
var app = angular.module('plunker', []);
app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.gridOptions = {
enableFiltering: false,
}
$scope.toggleFilter = function () {
$scope.gridOptions.enableFiltering = !$scope.gridOptions.enableFiltering;
//$scope.gridApi.core.notifyDataChange(uiGridConstants.dataChange.COLUMN);
};
});
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.3.x" src="https://code.angularjs.org/1.3.20/angular.js" data-semver="1.3.20"></script>
<script src="app.js"></script>
</head>
<body ng-controller="MainCtrl">
<div id="filter" ng-click="toggleFilter()">
({{gridOptions.enableFiltering ? "-" : "+"}}) {{gridOptions.enableFiltering ? "Dis" : "En"}}able Filtering
</div>
</body>
</html>
这是我的方法:https://jsfiddle.net/frishi/psysv1ao/2/
我认为您的三元表达式从未得到评估,您不需要使用$scope
在视图中访问其上的属性。这是暗示的。
其次,这是我的看法,控制DOM的渲染的三元物是非常不可读的。请考虑使用ng-if
或ng-show
来实现DOM的条件渲染。