Search code examples
angularjsbuttonangularjs-scope

angular button function run when the page loads


I have a ng-click button that as soon as the page loads it execute Save(); As soon as it load the page even before I click on Submit button, it add empty data to database, why it does not wait to be clicked? what can I do to fix it?

<div class="col-md-2">
   <button ng-click="Save()">Submit</button>
</div>

This is a part of my controller :

var App = angular.module("App", ['angularjs-dropdown-multiselect']);
App.controller('autoCompleteCTRL', ['$scope', '$rootScope', 'MyServices', function ($scope, $rootScope, MyServices) {

    $scope.Save = function () {
        var VCISel = angular.copy($scope.searchText);
        var RolesSel = angular.copy($scope.SelectedItems);
        MyServices.AddRole(Sel, "adad",RolesSel)
        .success(function (data) {
        })
        .error(function (error) {
            $scope.status = 'Unable to load customer data: ' + error.message;
        });
    };
    $scope.Save();
}]);

Solution

  • At the end of your controller definition, you are invoking the Save function by doing $scope.Save();