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

时间:2016-07-04 16:10:13

标签: angularjs button angularjs-scope

我有一个ng-click button,一旦页面加载它就执行Save(); 只要在我点击“提交”按钮之前加载页面,它就会将空数据添加到数据库, 为什么它不等待被点击? 我该怎么做才能解决它?

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

这是我的控制器的一部分:

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

3 个答案:

答案 0 :(得分:3)

在控制器定义的最后,您通过Save

调用$scope.Save();函数

答案 1 :(得分:2)

删除$scope.Save();控制器末尾的autoCompleteCTRL

答案 2 :(得分:1)

试试这个

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