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

I am unable to populate list on clicking Add button. Problem is when I change the text field again, My

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));
    };

}]);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744346222a4569700.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信