0

Happy day everyone,

I have started to work on angular-seed project. I have found jsfiddle with TODO example and would like to add another <input type-"text">

Is there any ways to split <input type-"text"> to make it look like two <input type-"text"> in the same row? My idea is similar to this jsfiddle made with jQuery, where merged string should be added to the <li> element

many thanks,

app.html

 <div ng-controller="MyCtrl">
        <input type="text" ng-model="enteredName" />
        <button ng-click="addName()">Add</button>
        <ul>
            <li ng-repeat="name in names">
                {{ name }}
                <button ng-click="removeName(name)">&times;</button>
            </li>
        </ul>
    </div>

app.js

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

function MyCtrl($scope) {
    $scope.names = ['batman', 'grumpy', 'yulu'];

    $scope.addName = function(){
        $scope.names.unshift($scope.enteredName);   
    }

    $scope.removeName = function(name) {
        var i = $scope.names.indexOf(name);
        $scope.names.splice(i, 1);
    }
}
o.O
  • 491
  • 1
  • 10
  • 26
  • see this http://stackoverflow.com/questions/32470928/angular-formly-adding-form-fields-dynamically-on-user-click/35603088#35603088 – Hadi J Apr 09 '17 at 10:07
  • You'd do it exactly the same way it's done in the JSFiddle. Make two input fields. – JJJ Apr 09 '17 at 10:10

1 Answers1

0

You don't want to make it "look like" two inputs, you actually use 2 inputs and change your array of strings to an array of objects with only slight changes in the view.

 $scope.names = [{first: 'bat',last: 'man'}];

View

<input type="text" ng-model="enteredName.first" />
<input type="text" ng-model="enteredName.last" />
<button ng-click="addName()">Add</button>
<ul>
   <li ng-repeat="name in names">
      First: {{ name.first }} Last: {{ name.last }}
      <button ng-click="removeName(name)">&times;</button>
   </li>
</ul>

DEMO

charlietfl
  • 170,828
  • 13
  • 121
  • 150