1

I've got some projects displayed in divs via ng-repeat. Each div contains a link with the id of the project.

Basically I want to bind my project id on ng-click in order to update a factory. This will allow me to share the clicked project's id with another controller who will load the project details data only.

Here is my projects view:

<div ng-repeat="detail in projects.details">
    <a ng-href="#!/project/project-details" ng-click="setId(detail.project_id)"><span></span></a>
</div>

Here is my factory:

app.factory('getGoodIdProjectDetails', function (){
    var savedId = 1; //Default à 1
    return {
    getId : function () {
       return savedId;
    },
    setId:function(idGet){
       savedId = idGet;
       return savedId;
    }
}      
});

Here are my controllers:

function ProjectCtrl($scope, $http, $timeout, getGoodIdProjectDetails) {
     $scope.setId = function (idGet) {
         $scope.idProjectDetails = getGoodIdProjectDetails.setId(idGet);
     };
}

function ProjectDetailsCtrl($scope, $http, $timeout, getGoodIdProjectDetails) {
    $scope.idProjectDetails = getGoodIdProjectDetails.getId();
}

In my view the console is displaying an error like I can't bind ng-click this way, but when I inspect the view it's sorting me the good thing, like ng-click="setId(8)"

I want the factory to update with the good id on click. Then I want to access this id in projectDetailsCtrl in order to load the project.

|| EDIT || : I changed the ng-click, that works fine, everything's good. Thx all

1
  • This is a perfect case for using a state based routing system like angular-ui router. In that case your id would simply be a stateParam or (depending on your backend), part of your url path. Commented Oct 7, 2014 at 17:33

1 Answer 1

3

try this:

<div ng-repeat="detail in projects.details">
    <a ng-href="#!/project/project-details" ng-click="setId(detail.project_id)"><span>Voir le projet ></span></a>
</div>

you don't need to use binding expression {{}} to pass values to functions

Sign up to request clarification or add additional context in comments.

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.