Evaluate or call directive on click in angular

I am trying to load an image from json and if image is not available, then show firstname and lastname.

I have controller like this:

app.controller('task1Controller',['$scope', 'taskFactory', '$state', 'imageTestService', function($scope, taskFactory, $state, imageTestService){ $scope.taskData = {}; $scope.current = 0; taskFactory.get().then(function(response){ $scope.jsonData = response.data.data.resultCareGivers; }); $scope.back = function(){ $scope.current = ($scope.current !== 0 ? $scope.current - 1 : 0); }; $scope.next = function(){ $scope.current = ($scope.current !== $scope.jsonData.length-1 ? $scope.current + 1 : $scope.jsonData.length-1); }; }]);

and a Directive to verify image loading:

app.directive('imageTestDirective', function($http){ return { restrict: 'A', link: function($scope, elem, attrs){ attrs.$observe('ngSrc', function(ngSrc){ if(ngSrc != null && ngSrc != ""){ $http.get(ngSrc).then(function(success){ $scope.noImage = false; console.log('success loading image', success); }, function(error){ $scope.noImage = true; console.log('error loading image', error); }); } }); } }; });

Html with attribute directive and next and back button to cycle through json:

<img image-test-directive ng-show="noImage === false" ng-src="{{jsonData[current].profilepic}}" alt=""/> <div ng-if="noImage"> <div>{{jsonData[current].firstName.charAt(1)+jsonData[current].lastName.charAt(1)}}</div> </div> <div class="col-xs-12"> <div class="col-xs-2"> <button type="button" ng-click="back()">Back</button> </div> <div class="col-xs-6" style="text-align: right"> <button type="button" ng-click="next()">Next</button> </div> </div>

The problem is the directive works on page load and the image is loaded properly, but I when I navigate through json object to view details, the directive is not evaluated (I mean when there no image inside json, it should show firstName+lastName)

How do I achieve it?


I think you wrote the logic too complicated for this use case.

In basic words JSON you get from Server contains some data with/without image URL or with broken image URL.

So instead to make HTML to be complicated, just embed your JSON in service and bring new JSON to your controller.

for example:

app.service('SomeService',['$q', /* ... */ function ($q /* ... */) { function MyNewJSON(origJSON){ // check here if origJSON contains wrong image url // and set new JSON key hasImage = true or false by validating it here in service and not in directive. this.hasImage = true; /* ... */ } function getNewJSON(){ return new MyNewJSON(origJSON); // it might be promise and you need resolve it in controller } }]);

So your HTML should look like:

<img ng-if="stateData.hasImage" ng-src="stateData.profilepic" alt=""/> <div ng-if="!stateData.hasImage"> <div >{{stateData.firstName.charAt(1)+stateData.lastName.charAt(1)}}</div> </div>

No directive needed.


Since image validation can take some time, you can put default image (some kind of placeholder)

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Evaluate or call directive on click in angular