1

so following is my Dynamically created template, it has an issue, when i add my template, it brings values from the previous DOM. Where was i want new to be empty.

Following is my HTML file, and my-custom-row-template is where i am repeating my template, now i assume that $index will create a new index, and values/DOM wouldn't repeat.

<section class="main_container">
        <div class="container">
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <form ng-submit="addNew()">
                                    <table class="table table-striped table-bordered">
                                        <thead>
                                        <tr>
                                            <th><input type="checkbox" ng-model="selectedAll" ng-click="checkAll()"/></th>
                                            <th scope="col">Setup Responses</th>
                                            <th>Add Condition</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr ng-repeat="personalDetail in personalDetails">
                                            <td scope="col">
                                                <input type="checkbox" ng-model="personalDetail.selected"/>
                                            </td>
                                            <td scope="col" class="col-xs-10">

                                                <div ng-repeat="condition_set in conditions track by $index" my-custom-row-template></div>
                                            </td>
                                            </td>
                                            <td scope="col">

                                                <input type="button" value="Add Condition" ng-click="addCondition()"
                                                       class="btn btn-primary addnew"/>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>

                                    <div class="form-group">
                                        <input ng-hide="!personalDetails.length" type="button"
                                               class="btn btn-danger pull-right"
                                               ng-click="remove()" value="Remove">
                                        <input type="button" class="btn btn-primary addnew pull-right" value="Add New" ng-click="addNew()">
                                        <input type="submit" class="btn btn-primary pull-right" value="Save">
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </section>
    </span>

And this is my template code

<div class="col-xs-8 pull-left">
    <div class="row form-group">
        <!--<select ng-model="response.condition" style="color: black;">-->
        <!--<option>Response Message</option>-->
        <!--<option>IF</option>-->
        <!--<option>Else</option>-->
        <!--</select>-->
        <select ng-model="selectedCondition[$index]">
            <option ng-repeat="x in conditions.condition_set" value="{{x.name}}">{{x.value}}</option>
        </select>

        <input ng-show="selectedCondition=='0'" type="text" class="form-control"
               ng-model="personalDetail[$index].message"/>
        <input ng-show="selectedCondition=='1'" type="text" class="form-control"
               ng-model="conditions[$index].response"/>
        <input ng-show="selectedCondition=='2'" type="text" class="form-control"
               ng-model="conditions[$index].elseMessage"/>
        <select ng-show="selectedCondition=='1'" ng-model="goToStepOrNew">
            <option ng-repeat="x in conditions[$index].create_new_conditions" value="{{x.name}}">{{x.value}}</option>
        </select>

        <input type="button" value="Remove Condition" ng-click="remove_condition($index)" class="btn btn-danger"/>
    </div>

</div>

And this is how i am handling it within my controller

$http({
                method: "POST",
                url: "team_lead_showed_contacts/ajax",
                data: $.param(obj),
                headers: {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
                transformResponse: [function (data) {
                    var str = '';
                    str = data.toString();
                    str = str.replace(/\\/g, 0);
                    return str;
                }]
            }).then(function (response) {
                //debugger;

                $scope.campaigns = JSON.parse(response.data);
                $scope.personalDetails = [
                    {
                        'add_tempalte': '<div my-custom-row-template> </div>',
                    }
                ];

                $scope.addNew = function (personalDetail) {
                    $scope.personalDetails.push({
                        'add_template': '<div my-custom-row-template> </div>',
                    });
                };


                function triggerNewRow($scope) {
                    if ($scope.goToStepOrNew == 0) {

                    }
                }

                $scope.remove = function () {
                    var newDataList = [];
                    $scope.selectedAll = false;
                    angular.forEach($scope.personalDetails, function (selected) {
                        if (!selected.selected) {
                            newDataList.push(selected);
                        }
                    });
                    $scope.personalDetails = newDataList;
                };

                $scope.checkAll = function () {
                    if (!$scope.selectedAll) {
                        $scope.selectedAll = true;
                    } else {
                        $scope.selectedAll = false;
                    }
                    angular.forEach($scope.personalDetails, function (personalDetail) {
                        personalDetail.selected = $scope.selectedAll;
                    });
                };


                $scope.conditions = [];
                $scope.conditions.push('myCustomRowTemplate');
                $index = 0;
                $scope.addCondition = function () {
                    $scope.conditions.push('myCustomRowTemplate');
                };
                $scope.conditions.condition_set = [
                    {name: 0, value: 'Response Message'},
                    {name: 1, value: 'IF'},
                    {name: 2, value: "ELSE"}
                ];
                $scope.conditions.create_new_conditions = [
                    {name: 0, value: 'Create Step'},
                ];
                $scope.remove_condition = function (element) {
                    $scope.conditions.splice(element, 1);
                    // console.log(element);
                };

            });

For Reference i am attaching an image to give an idea, to what happens when i copy Add New. Copy of already present Elements is pushed into the DOM. Check Image to see duplication of content.

EDIT Tried some thing like this following Kinda same issue like this post but no effect.

$scope.personalDetails = [
                        {
                            'add_template': '<div my-custom-row-template> </div>',
                        }
                    ];
                    var newStep = angular.copy($scope.personalDetails);
                    $scope.addNew = function (personalDetail) {

                        $scope.personalDetails.push(
                            newStep
                        );
                    };
noobie-php
  • 6,817
  • 15
  • 54
  • 101
  • Somethings in the template don't look right, is selectedCondition an array? You access it like selectedCondition[$index], and then use selectedCondition == '1' as a condition check. I'm not sure that makes sense... – Stradosphere May 02 '18 at 19:59
  • yes this isnt correct infact [$index] used here dosent reflect any change, but i simulated it here to give an idea as what i actually want, i just want my DOM to be dynamic. – noobie-php May 02 '18 at 20:01
  • The directive is reflecting the $scope values from the controller because by default that is how the $scope is inherited. Take a look at this particular link for more info: https://stackoverflow.com/questions/14050195/what-is-the-difference-between-and-in-directive-scope-in-angularjs – Stradosphere May 02 '18 at 20:09
  • @Stradosphere: so what you are referring to is actually change in scope? – noobie-php May 02 '18 at 20:12
  • You can either have your directive inherit the $scope which is default and the data will be the same, or you can have the directive use isolated $scope so each instance has its own copy, which I think it what you are trying to do. But I don't see your directive above... – Stradosphere May 03 '18 at 15:17
  • When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. That code should be… **Minimal** – Use as little code as possible that still produces the same problem, See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – georgeawg May 03 '18 at 15:49
  • @georgeawg: i agree to this, and i also follow same practice, but on similar question that i asked, i was asked to put more code, and after number of comments i was told now it makes sense. Other then that i totally endorse your point here – noobie-php May 03 '18 at 17:50
  • **Divide and conquer**. When you have a small amount of code, but the source of the problem is entirely unclear, start removing code a bit at a time until the problem disappears – then add the last part back. – georgeawg May 03 '18 at 21:09

0 Answers0