在angularjs中隐藏和显示

时间:2013-10-04 07:21:16

标签: javascript angularjs button styles visible

我是angularJs的新手。

这是我的位级视图

<input type="text" ng-model="todoName" size="30"
            placeholder="Add Your Name">
        <input type="text" ng-model="todoAge" size="30"
            placeholder="Add Your Age">
        <input type="hidden" ng-model="todoId" />
        <form style="display:'?????????'" ng-submit="addTodo()">
            <input class="btn-primary" type="submit" value="add">
        </form>
         <form style="display:'?????????'" ng-submit="addTodo()">
            <input class="btn-primary" type="submit" value="update">
        </form>

这是我的angularjs位级编码

$scope.DisplayUpdate = "none";

我有一个用于管理学生详细信息的screan,我想在第一次显示添加按钮时隐藏更新按钮,当更新时间显示更新按钮时,此时添加按钮需要隐藏。

看到此行设置隐藏并使用角度js在脚本端显示详细信息:$scope.DisplayUpdate = "none";

如何在我的按钮样式中设置此值?

 <form style="display:'?????????'" ng-submit="addTodo()">
                <input class="btn-primary" style="display:'?????????'" type="submit" value="add">
            </form>

3 个答案:

答案 0 :(得分:4)

您可以使用ng-show隐藏它

<form ng-show="DisplayUpdate === 'none'" ng-submit="addTodo()">
    <input class="btn-primary" type="submit" value="add">
</form>

如果要从DOM树中删除它,请使用ng-if

<form ng-if="DisplayUpdate === 'none'" ng-submit="addTodo()">
    <input class="btn-primary" type="submit" value="add">
</form>

答案 1 :(得分:1)

不要直接设置样式,使用ng-show指令让Angular为你处理它。

API Docs

答案 2 :(得分:0)

最后我明白了。

以下是我的代码:

JS档案:

function TodoCtrl($scope) {

    var value = BindStudentList();
    function BindStudentList() {
        $.ajax({
            url: '/Home/Contact1',
            type: 'GET',
            async: false,
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                value = data.data;
            }
        });
        $scope.DisplaySave = true;
        $scope.DisplayUpdate = false;
        return value;
    }

    $scope.addTodo = function () {      
        $.ajax({
            url: '/Home/Index1',
            type: 'GET',
            data: { todoName: $scope.todoName, todoAge: $scope.todoAge },
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                value = data.data;
            }
        });
    };

    $scope.Sample = value;
    $scope.remaining = function () {
        var count = 0;
        angular.forEach($scope.Sample, function (todo) {
            count += todo.done ? 0 : 1;
        });
        return count;
    };

    $scope.editTodo = function (Student) {
        $.ajax({
            url: '/Home/Edit1',
            type: 'GET',
            data: { Id: Student.todo.StudentId },
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success: function (data) {
                $scope.todoName = data.data.StudentName;
                $scope.todoAge = data.data.StudentAge;
                $scope.todoId = data.data.StudentId;
                $scope.DisplayUpdate = true;
                $scope.DisplaySave = false;
            }
        });
    };
}

这是我的观看代码

<!doctype html>
<html ng-app>

<body>
    <script src="~/Scripts/angular.js"></script>

    <h2>Student Details</h2>
    <div ng-controller="TodoCtrl">
        <span>{{remaining()}} of {{Sample.length}} remaining</span>
        [ <a href="" ng-click="archive()">archive</a> ]
        <input type="text" ng-model="todoName" size="30"
            placeholder="Add Your Name">
        <input type="text" ng-model="todoAge" size="30"
            placeholder="Add Your Age">
        <input type="hidden" ng-model="todoId" />
        <form ng-show="DisplaySave" ng-submit="addTodo()">
            <input class="btn-primary" type="submit" value="Save">
        </form>
        <form ng-show="DisplayUpdate" ng-submit="addTodo()">
            <input class="btn-primary" type="submit" value="Update">
        </form>
        <br />
        <br />
        <table>
            <tr>
                <td><b>Student Name</b></td>
                <td><b>Student Age</b></td>
            </tr>
            <tr ng-repeat="todo in Sample">
                <td><span>{{todo.StudentName}}</span></td>
                <td><span>{{todo.StudentAge}}</span></td>
                <td>
                    <button value="{{todo.StudentId}}" ng-click="editTodo(this)">Edit</button>
                </td>
            </tr>
        </table>
    </div>
    <script src="~/Scripts/Todo.js"></script>
</body>
</html>

我在我的按钮中添加了ng-show="DisplaySave"ng-show="DisplayUpdate",我将使用<在 javascript 中传递truefalse等值编辑和加载时间时强> angularjs 。

现在它正在运作。我知道我的代码会对其他人有所帮助。欢呼声