0

So I'm trying to use angularjs ui.boostrap rating component with my app, however i have no idea how to pass from ng-repeat rating value to ratingCtrl. I have tried using ng-init, but it just returns undefined error... Any ideas? (Data should be passed by r.rating)

My template:

<div class="row">
    <div class="col-md-4" id="recipe-list" ng-repeat="r in recipes track by $index">
      <a href="show/{{r.recipe.id}}"><img ng-src="{{r.recipe.image}}"></a>
      <h4><a href="show/{{r.recipe.id}}">{{r.recipe.name}}</a></h4>
      <ul class="list-inline list-ingredients">
        <li ng-repeat="ingredient in r.recipe.ingredients">{{ingredient.name}}</li>
      </ul>
      <div ng-controller="RatingCtrl">
        <rating class="rating" ng-style="success" ng-model="rate" max="max" readonly="isReadonly" on-hover="hoveringOver(value)" on-leave="overStar = null" ng-click="setRating(r.recipe.id)"></rating>

      </div><!-- rating controller end-->
    </div>
  </div>

My controller:

recipeApp.controller('RatingCtrl', function ($scope, $http) {

    $scope.max = 5;
    $scope.isReadonly = false;

    $scope.ratingStates = [
        {stateOn: 'glyphicon-ok-sign', stateOff: 'glyphicon-ok-circle'},
        {stateOn: 'glyphicon-star', stateOff: 'glyphicon-star-empty'},
        {stateOn: 'glyphicon-heart', stateOff: 'glyphicon-ban-circle'},
        {stateOn: 'glyphicon-heart'},
        {stateOff: 'glyphicon-off'}
    ];

    $scope.setRating = function(id) {

        var data = {
            rating: $scope.rate,
            id: id
        };

        $http.post('api/rate/recipe', angular.toJson(data), {cache: false})
            .success(function(data){

            })
            .error(function(data){
                alert(data.message);
            });
    };
});
scniro
  • 16,844
  • 8
  • 62
  • 106
  • create a demo on http://plunkr.co/edit and post it here so we can see it in action. In general to access a parent scope you can use `$scope.$parent`(*see http://stackoverflow.com/questions/21453697/angularjs-access-parent-scope-from-child-controller for more info*) – Gabriele Petrioli Nov 21 '14 at 12:10
  • Are you trying to only display rating upon click? – kubuntu Nov 21 '14 at 12:57
  • Your code seems fine. Could it be that your web service does not work as expected? – kubuntu Nov 21 '14 at 14:25

0 Answers0