2

我需要使用控制器中的功能更改 ng-init 值

<div ng-app ng-controller="FilterManager" data-ng-init="FilterVisible=false;" >
    <p>name: {{ FilterVisible}}</p>
    <a href="javascript:void(0);" data-ng-click="setFilter();">click</a>
</div>

<script type="text/javascript">
function FilterManager($scope) {
    $scope.setFilter = function() {
        FilterVisible=true;
    };
}

</script>

为什么这段代码不起作用?谢谢

@PSL 您的代码运行良好,但是如果我在 ng-repeat 中,我该怎么做呢?

<div ng-app="myApp" ng-controller="MainCtrl">
    <div ng-repeat="c in columns">
        <div ng-init="FilterVisible=false" data-ng-click="setFilter();">{{c.name}} {{FilterVisible}}</div>
    </div>
</div>
<script type="text/javascript">
var app = angular.module('myApp', []);
app.controller('MainCtrl', function($scope) {
    $scope.columns = [{'name': 1}, {'name': 2}, {'name': 3}];
    $scope.setFilter = function() {
        FilterVisible=true;
    };
});

4

0 回答 0