最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - AngularJS: Add Elements to <li> dynamically - Stack Overflow

matteradmin2PV0评论

I am unable to populate list on clicking Add button. Problem is when I change the text field again, My List data gets changed (binding), how to avoid that?

HTML

<div class="control-group">
    <label class="control-label" for="projectManager">Project Manager(s)</label>
    <div class="row controls" >
        <input type="text" class="span3" placeholder="Manager name" ng-model="pm.name">
        <input type="text" class="span2" placeholder="Manager type" ng-model="pm.type">
        <button type="button" ng-click="addManagersForm()">&nbsp;&nbsp;Add&nbsp;&nbsp;</button>
    </div>
    <div class="row controls" >
        <ul><li ng-repeat="tPm in pms">{{tPm.name}}</li></ul>
    </div>
</div>

JS

app.controller('myContrl',["$scope", "$http", function($scope, $http) {
    $scope.pms = [];

    $scope.addManagersForm = function() {
        console.log($scope.pm);
        $scope.pms.push($scope.pm);
    };

}]);

I am unable to populate list on clicking Add button. Problem is when I change the text field again, My List data gets changed (binding), how to avoid that?

HTML

<div class="control-group">
    <label class="control-label" for="projectManager">Project Manager(s)</label>
    <div class="row controls" >
        <input type="text" class="span3" placeholder="Manager name" ng-model="pm.name">
        <input type="text" class="span2" placeholder="Manager type" ng-model="pm.type">
        <button type="button" ng-click="addManagersForm()">&nbsp;&nbsp;Add&nbsp;&nbsp;</button>
    </div>
    <div class="row controls" >
        <ul><li ng-repeat="tPm in pms">{{tPm.name}}</li></ul>
    </div>
</div>

JS

app.controller('myContrl',["$scope", "$http", function($scope, $http) {
    $scope.pms = [];

    $scope.addManagersForm = function() {
        console.log($scope.pm);
        $scope.pms.push($scope.pm);
    };

}]);
Share Improve this question edited Nov 21, 2013 at 13:08 Maxim Shoustin 77.9k29 gold badges210 silver badges228 bronze badges asked Nov 21, 2013 at 12:58 rohitpalrohitpal 4551 gold badge6 silver badges21 bronze badges 1
  • You have mis-matched quotes in your example. Is that just a typo? – Rory McCrossan Commented Nov 21, 2013 at 12:58
Add a ment  | 

2 Answers 2

Reset to default 2

It happens because you are pushing the $scope.pm object into the array, and that object is binded in the form.

Just create a new object and you will be fine:

$scope.addManagersForm = function() {
    var pm = $scope.pm;
    $scope.pm = {}; // Do this to clean up the form fields
    $scope.pms.push(pm);
};

It's because instances are passed by reference. You can use angular.copy to create a deep copy of it.

See this example Plunker: http://plnkr.co/edit/d8HwXzTBK61sMuwLollW

The updated code:

HTML page

  <body ng-app="app" ng-controller="myContrl">
    <div class="control-group">
    <label class="control-label" for="projectManager">Project Manager(s)</label>
    <div class="row controls" >
        <input type="text" class="span3" placeholder="Manager name" ng-model="pm.name">
        <input type="text" class="span2" placeholder="Manager type" ng-model="pm.type">
        <button type="button" ng-click="addManagersForm(pm)">&nbsp;&nbsp;Add&nbsp;&nbsp;</button>
    </div>
    <div class="row controls" >
        <ul><li ng-repeat="tPm in pms">{{tPm.name}}</li></ul>
    </div>
</div>

JavaScript

angular.module('app', []).controller('myContrl',["$scope", "$http", function($scope, $http) {
    $scope.pms = [];

    $scope.addManagersForm = function(pm) {
        console.log(pm);
        $scope.pms.push(angular.copy(pm));
    };

}]);
Post a comment

comment list (0)

  1. No comments so far