Error clicking a button in Ionic framework

I am trying to learn the Ionic Framework.

This can be a very simple question for someone who knows this structure. But I can not do this job.

I just need to show a warning when a button is clicked.

My index.html code contains a button:

<div class="bar bar-footer">
    <div class="title">
        <button class="button button-light" ng-click="showAlert()">
        Origin
        </button>
    </div>
</div>

This is my js code:

angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope) {
    {
        // Use our scope for the scope of the modal to keep it simple
        scope: $scope
    }
});

$scope.showAlert = function() {
    alert("show");
};

I get this error when a button is clicked:

Uncaught ReferenceError: $scope is not defined app.js:9
(anonymous function)

Any idea what I'm doing wrong?

+4
source share
1 answer

change your code

angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope) {
    {
        // Use our scope for the scope of the modal to keep it simple
        scope: $scope
    }
});

$scope.showAlert = function() {
    alert("show");
};

to

angular.module('todo', ['ionic'])
.controller('TodoCtrl', function($scope) {
    {
        // Use our scope for the scope of the modal to keep it simple
            $scope.showAlert = function() {
                  alert("show");
             };
    }
});
+4
source

Source: https://habr.com/ru/post/1533283/


All Articles