1

https://prnt.sc/k2z0w9

Hey. i have a ng repeat table (Sunday is a table)

and i have a function that takes a worker ID and returns a worker name that called GetWorkerName How do i call the function in the ng repeat without clicking the function? pls help me :)

        $scope.GetWorkerName=function(workerid){
        $http.get("/ShiftWeb/rest/worker/getworkername?id="+workerid)
            .then(function(response){
                $scope.getWorkerName=response.data;
            })
        };

HTML:

--

<table class="table table-bordered table-striped">
<tr>
    <th>Sunday</th>
</tr>
<tr ng-repeat="settingtime in settingtimes | orderBy:'time' | 
filter:by_shiftDay" ng-if="settingtime.shiftDay=='SUNDAY'">
    <td>
    {{settingtime.time}}
    </br>
    <div style="border: 1px solid red" ng-repeat="sidor in nextsidor" ng- 
 if="sidor.shiftTime==settingtime.id">
    Worker: {{sidor.worker}}
    {{getWorkerName}}
    </br>
    Role : {{sidor.role}}

    </br><button class="btn btn-primary" ui-sref="sidor" ng- 
click="RemoveShift(sidor.id)">Remove Shift</button>
    </div>
    </br><button class="btn btn-primary" ui-sref="sidor" ng- 
click="CreateShift(settingtime.id)">Create Shift</button>
</tr>
</table>

1 Answer 1

1

What you probably want to do is create a directive that can take the ID and return whatever data you want. Then you can reuse this in other places.

Using the directive:

<worker id="231"></worker>

Defining the directive:

(function(){

    angular.module('example').directive('worker', ['workerService', function(workerService){

        function link($scope, element, attrs) {
            $scope.workerName = workerService.getName(attrs.id);
        };

        return {
            template: '(<span>{{ workerName }}</span>)',
            link: link
        };

    }]);

})();
Sign up to request clarification or add additional context in comments.

2 Comments

thank you very much can you please tell me what is attrs?
attrs is the attributes that get passed in on the directive. In this case just the id. But you can pass in more attributes if you wanted, and then they would be available on the attrs object in this example.

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.