如何使用函数设置/更改ng-init值?

时间:2014-10-27 17:40:28

标签: javascript angularjs ng-init

我需要使用控制器中的函数更改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;
    };
});

0 个答案:

没有答案