简体   繁体   English

如何在AngularJs中为添加/编辑用户重用HTML / Controller

[英]How to reuse HTML/Controller for Add/Edit user in AngularJs

I have two separate HTML templates for Add / Edit user and hence two separate controllers. 我为添加/编辑用户有两个单独的HTML模板,因此有两个单独的控制器。 How i can have one HTML file and one controller for both Add/Edit functionality. 我如何同时具有一个HTML文件和一个用于添加/编辑功能的控制器。

Add User 添加用户

  <div>
       <label>FirstName</label>
                    <div>
         <input type="text" name="name"  data-ng-model="register.Student.FirstName">
                    </div>
                </div>
                <div>
                    <label>LastName</label>
                    <div>
         <input type="text" name="name" data-ng-model="register.Student.LastName">
                    </div>
</div>

Edit User 编辑用户

  <div>
                    <label>FirstName</label>
                    <div>
        <input type="text" name="name" data-ng-model="student.Student.FirstName">
                    </div>
                </div>
                <div>
                    <label>LastName</label>
                    <div>
         <input type="text" name="name" data-ng-model="student.Student.LastName">
                    </div>
</div>

AddController which initialize FirstName and Last Name to null value. AddController ,用于将名字和姓氏初始化为空值。 This is not the case in my EditController. 在我的EditController中不是这种情况。

       $scope.register = {
                User: {
                    FirstName: '',
LastName: '',
            };

My route configuration 我的路线配置

 templateUrl: '/app/users/newStudent.html',
        controller: 'newStudentController',
        controllerUrl: '/app/users/newStudentController.js', 

       templateUrl: '/app/users/editStudent.html',
        controller: 'editStudentController',
        controllerUrl: '/app/users/editStudentController.js',

Club both the html files and name it say student.html and club both the controller files and name it say student.js . student.html html文件并命名为student.htmlstudent.html controller文件并命名为student.js

student.js student.js

app.module('myApp', []).controller('studentController', function () {})

Your route config: 您的路线配置:

templateUrl: '/app/users/student.html',
        controller: 'studentController',
        controllerUrl: '/app/users/student.js',

UPDATE UPDATE

DEMO: http://jsfiddle.net/softvar/uh17tseh/1/ 演示: http : //jsfiddle.net/softvar/uh17tseh/1/

HTML : HTML

<div ng-app="myApp" ng-controller="myController">
    <div ng-repeat="stud in students">
        <div ng-hide="isEditForm">
            <span>{{stud.name}} - {{stud.age}}</span>
            <button ng-click="isEditForm=true;">Edit Student</button>
        </div>
        <div ng-show="isEditForm">
            <label>Enter Name</label><input ng-model="stud.name" />
            <label>Enter Age</label><input ng-model="stud.age" />
            <button ng-click="isEditForm=false;">Save</button>
        </div>
    </div>
    <br/><br/>

    <div ng-hide="isAddForm">
        <button ng-click="isAddForm=true;">Add Student</button>
    </div>
    <div ng-show="isAddForm">
        <label>Enter name</label><input ng-model="newStudent.name" />
        <label>Enter Age</label><input ng-model="newStudent.age" />
        <button ng-click="addStudent()">Add</button>
        <button ng-click="isAddForm=false;">Cancel</button>
    </div>
</div>

JS : JS

var app = angular.module('myApp', []);
app.controller('myController', function ($scope) {
    $scope.students = [
        {"name": "ABC", "age": 21},
        {"name": "DEF", "age": 22},
        {"name": "GHI", "age": 23},
        {"name": "XYZ", "age": 24}
    ];
    $scope.newStudent = {};
    $scope.addStudent = function () {
        if ($scope.newStudent.name && $scope.newStudent.age) {
            $scope.students.push({"name": $scope.newStudent.name,"age": $scope.newStudent.age});
            $scope.isAddForm = false;
        }
    };
});

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

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