0

I am using directive to add HTML div dynamically on the click of button.

  1. How can I bind the model to controller scope object?
  2. How can populate the scope object with enter data in dynamically added div?
  3. How can I get list of object while click on save button.

Please find the code that I tried to add html div dynamically, but doesn't have any idea how to bind the entered data to scope object.

  <html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.8/angular.min.js"></script>
        <script type="text/javascript">

        function FlatMember() {
            this.firstName  =  '';
            this.lastName  =  '';
            this.emailId  =  '';
            this.panNo  =  '';
            this.phoneNo  =  '';
            this.profileImage  =  ''
        }

        angular.module('myApp', []);

        angular.module('myApp').controller('FlatMemberController', function ($scope) {
            $scope.flatMembers = [];

            $scope.addFlatMember = function() {
                $scope.flatMembers.push(new FlatMember());
            };

            $scope.SaveFlatMember = function(){
                console.log($scope.flatMembers);
            }
        });

        angular.module('myApp').directive('memberInformation', function(){
            return {
                restrict: 'A',
                template: '<div style="margin-top: 50px;">\
                            <div>\
                                <p>\
                                    <label>First Name </label>\
                                    <input type="text" id="firstName" /> \
                                    <label>Last Name </label> <input type="text" id="lastName" />\
                                </p>\
                                <p>\
                                    <label>Email </label> <input type="email" id="emailId"/>\
                                </p>\
                                <p>\
                                    <label>PAN Number </label> <input type="text" id="panNo" data-ng-minlength="10"/>\
                                </p>\
                                <p>\
                                    <label>Mobile </label> <input type="text" data-ng-minlength="10" id="phoneNo" />\
                                </p>\
                            </div> </div>',
                replace: true,
                transclude: false,
                scope: {
                    memberInfo: '=memberInformation'
                }
            };
        });
        </script>
    </head>
    <body ng-app="myApp">
        <div ng-controller="FlatMemberController">
            <button ng-click="addFlatMember()">Add new Member</button>
            <div ng-repeat="flatMember in flatMembers"member-information="flatMember"></div>
            <button ng-click="SaveFlatMember()">Save Member</button>
        </div>
    </body>
</html>

1 Answer 1

0

Your template is missing the ng-model bindings:

angular.module('myApp').directive('memberInformation', function(){
            return {
                restrict: 'A',
                template: '<div style="margin-top: 50px;">\
                            <div>\
                                <p>\
                                    <label>First Name </label>\
                                    <input type="text" id="firstName" name="firstName" ng-model="memberInfo.firstName" /> \
                                    <label>Last Name </label> <input type="text" id="lastName" name="lastName" ng-model="memberInfo.lastName" />\
                                </p>\
                                <p>\
                                    <label>Email </label> <input type="email" id="emailId" name="emailId" ng-model="memberInfo.emailId" />\
                                </p>\
                                <p>\
                                    <label>PAN Number </label> <input type="text" id="panNo" data-ng-minlength="10" name="panNo" ng-model="memberInfo.panNo" />\
                                </p>\
                                <p>\
                                    <label>Mobile </label> <input type="text" data-ng-minlength="10" id="phoneNo" name="phoneNo" ng-model="memberInfo.phoneNo" />\
                                </p>\
                            </div> </div>',
                replace: true,
                transclude: false,
                scope: {
                    memberInfo: '=memberInformation'
                }
            };
        });
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @fedeisas. If I have clicked Save button, how can get the list of object?
Open your console. You are already doing console.log of your $scope.flatMembers object. I'm not sure I understand your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.