AngularJs按钮失去作用域

AngularJs button loses scope

本文关键字:作用域 失去 按钮 AngularJs      更新时间:2023-09-26

我的页面上的一个按钮出现$scope问题。页面上有一个ui网格。当单击"添加新记录"按钮时,它会隐藏网格并显示允许用户输入新记录的div。一旦用户单击"保存"按钮保存他们输入的信息,屏幕就会返回到原始状态,显示网格,隐藏其他div。问题是我再也不能点击"添加新"按钮了,因为它已经失去了$scope。

这是我的HTML:

<div ng-app="myModule" ng-controller="OperatorCtrl">
    <div class="panel panel-primary">
        <div class="panel-heading">Business Unit Administration</div>
        <div class="panel-body">
            <div ng-hide="noView">
                @Html.Label("Edit a Business Unit belo0w to assign Users and Contractors")
                <div>
                    <div ui-grid="gridOptions" class="grid" ng-style="{height: (gridOptions.data.length*30)+32+'px'}" ui-grid-auto-resize></div>
                </div>
                <div class="pull-right">
                    <button type="button" class="btn btn-primary" style="width:100px" ng-click="addNew()">Add New</button>
                </div>
            </div>
 <div ng-show="noView">
                <form name="AddBusinessUnitForm">
                    <table style="width:100%">
                        <thead>
                            <tr>
                                <th>Business Unit Name</th>
                                <th>Operator System ID</th>
                                <th>Action</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td><input type="text" style="width:200px" ng-model="form.vchDescription" /></td>
                                <td><input type="text" style="width:300px" ng-model="form.vchOperatorSystemID" /></td>
                                <td ng-show="addNew"><button type="button" class="btn btn-info btn-sm" ng-click="saveNew()">Save</button></td>
                                <td>
                                    <button type="button" class="btn btn-primary" style="width:100px" ng-click="return()">Close</button>
                            </tr>
                        </tbody>
                    </table>
                </form>

"添加新内容"和"关闭"按钮仅切换"noView"变量以显示和隐藏每个部分。

这是我的Angular:

$scope.addNew = function () {
    $scope.noView = true;
};
$scope.saveNew = function () {
    $scope.businessUnitName = $scope.form.vchDescription;
    operatorService.savenew($scope.form)
    .success(function (data) {
        $scope.businessUnitID = data;
        getUsers($scope.businessUnitID);
        $scope.viewTabs = true;
        $scope.addNew = false;
    });
};
$scope.return = function () {
    $scope.noView = false;

};

我是Angular的新手,不知道如何维护按钮的作用域。

非常感谢您的帮助!

在html中,您没有向使用ng-click的函数传递任何内容。因为没有什么可以保存,所以什么都没有保存。我希望这能有所帮助。