0

For a list of items I want to show details after item click. Details will be loaded with ajax request. I have something similar to this: http://jsfiddle.net/asmKj/ How to modify this to work with details loaded dynamically?

For sure I have to prepare function in my controller like this:

$scope.getDetails = function (name) {
    return $scope.details = myService.getDetails(name).then(function (details) {
        return $scope.details = details;
    });
}

But how to bind this data to details div?

2
  • would be nice if you show us how details and showDetails are actually structured? and if it is possible to reorganize this structure... (if needed of course) Commented Dec 2, 2013 at 23:50
  • go through tutorial on docs site. It is perfect example of what you want Commented Dec 2, 2013 at 23:58

2 Answers 2

2

I would rather change with something like this:

HTML

<ul class="procedures" ng-app ng-controller="sample">
    <li ng-repeat="procedure in procedures">
        <h4><a href="#" ng-click="getDetails($index)">{{procedure.definition}}</a></h4>
         <div class="procedure-details" ng-show="procedures.isVisible">
            <p>Number of patient discharges: {{procedure.discharged}}</p>
         </div>
    </li>
</ul>  

JS

$scope.procedures = [
    {
        definition: 'Procedure 1',
        discharged: 23
    },
    {
        definition: 'Procedure 2',
        discharged: 2
    },
    {
        definition: 'Procedure 3',
        discharged: 356
    }
];

$scope.getDetails = function ($index) {
    $http.get('your-url').success(
       // use the data retrieved
       procedures[$index].isVisible = !procedures[$index].isVisible;
    );
}
Sign up to request clarification or add additional context in comments.

Comments

1

There are basically two options.

  1. Put everything in your procedure objects as separate properties (when the details are loaded) -- then you can just use procedure.showDetails in ng-repeat.

  2. Use $index to get index from your procedures -- then you can use it to access any arbitrary collection from your scope in ng-repeat.

(may update my answer, when you provide more info about structure of your data; and if still needed)

Comments

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.