简体   繁体   English

页面加载时运行角度按钮功能

[英]angular button function run when the page loads

I have a ng-click button that as soon as the page loads it execute Save(); 我有一个ng-click button ,页面加载后立即执行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();
}]);

在控制器定义的最后,您通过执行$scope.Save();调用Save函数$scope.Save();

Remove $scope.Save(); 删除$scope.Save(); at end of autoCompleteCTRL controller. autoCompleteCTRL控制器的末尾。

Try this 尝试这个

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;
        });
    };
}]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM