0

If I have a JSON input like this:

$scope.managers = 
[
    {
        managerName : "Sam",
        employeesList: [
        { employeeName: 'Joe', employeeCategory: 'Developer' },
        { employeeName: 'Bob', employeeCategory: 'Developer' }
        ]
    }
]

I want to bind all the employeeNames for that manager to one comma separated input something like this:

<div ng-repeat="manager in managers">
    <input type="text" ng-model="manager.employeesList.employeeName" ng-list />
</div>

So that I can just enter

John, Bob

in the input field.

What is the best way to do this?

(I cannot change the structure of the JSON object)

2
  • Back to the actual question; I suspect the only way to do what you want is going to be by writing some the parsing code of the user's input in order to save it in the proper places. Commented May 13, 2014 at 20:32
  • @JeffryHouser any suggestions on the best way to do that? Commented May 13, 2014 at 20:47

1 Answer 1

0

I would create a button that on ng-click="storeNames(nameInput, manager).

.split() takes a string and separates it into elements of an array every time it sees a selector (which in this case is ', ')

http://www.w3schools.com/jsref/jsref_split.asp

    $scope.nameInput = '';
    $scope.managers = [
            {
            managerName: 'Billy Joe',
            employeesList: [
                { employeeName: 'Joe', employeeCategory: 'Developer' },
                { employeeName: 'Bob', employeeCategory: 'Developer' }
                ]
            }
        ];

    $scope.storeNames = function(names, manager) {
        if(names.length > 0) {
            names.split(", ");
            for(var x = 0; x < names.length; x++) {
                manager.employeesList.employeeName[x] = names[x];
            }
        }
    }
Sign up to request clarification or add additional context in comments.

7 Comments

correct. i did this already. that is not the issue. i will edit the question to clarify
what I am trying to do is input all the employee names for that manager in one input field, i.e. Joe, Bob
I'm putting together a fiddle for you real quick.
actually i ended up just editing my answer. Let me know what you think.
I've edited it again to fix a couple typos. I'm also pretty new to angular but have been using it professionally for about 4 months now. So I'd also learn a lot from helping you out if you need any more.
|

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.