为什么简单的功能不起作用?

时间:2016-05-26 05:34:32

标签: angularjs

我在angularjs控制器中编写一个简单的函数,打开/关闭bool变量,但它不起作用?为什么呢?

app.controller("registrationCtrl", function ($scope) {
    $scope.user = {};
    $scope.wasSubmitted = false;
    $scope.showInfo = true;

    $scope.submit = function () {
        $scope.wasSubmitted = true;
    };

    $scope.switch = function () {
        if ($scope.showInfo == true) {
            $scope.showInfo = false;
        } else {
            $scope.showInfo = true;
        }
    };
});

该功能使用如下:

<button type="button" onclick="switch()">Show/Hide</button> {{showInfo}}

2 个答案:

答案 0 :(得分:3)

使用ng-click:

<button type="button" ng-click="switch()">Show/Hide</button>

答案 1 :(得分:2)

您在视图中遇到问题。而不是onclick,有必要使用ng-click

<button type="button" ng-click="switch()">Show/Hide</button> {{showInfo}}