3

I am trying to just write jquery inside angularjs function.but don't know why it is not working?

HTML

<div>
   <span ng-repeat="image in post.postImages" ng-if="$index <= 3" ng-init="image.showDeleteIcon= false" ng-mouseover="image.showDeleteIcon=true" ng-mouseleave="image.showDeleteIcon=false">
   <span ng-if="$index == 3" ng-init="setImgCounter(post.postImages.length,$parent.$index+''+$index)"></span>
   <a id="{{$parent.$index}}{{$index}}" onclick="SetDataGallery(this.id)" ng-href="../upload/post-photos/{{image.attachmentURL}}">
   <img class="img-responsive feed-photo" ng-src="../upload/post-photos/{{image.attachmentURL}}" alt="Photo" style="display:inline;">
   <a href="#" class="imgDelete" ng-if="post.timelineStrore.hasControl" ng-show="image.showDeleteIcon" title="Delete Photo" ng-click="DeletePostAttachment(post.timelineStrore.id, image.postAttachmentId,'image')">
   <i class="fa fa-close"></i>
   </a>
   </a>
   </span>
</div>

JS

$scope.setImgCounter = function (counter,id)
{
    $("#" + id + " img:first-child").css("opacity", "0.5");
}

but its not working?

I also tried using =>

.att('style','opacity:0.5');
10
  • Where you are calling setImgCounter? Commented Jun 9, 2017 at 6:25
  • i recommend you don't use jq in angular code. so try use $scope.$apply(); after jquery code. Commented Jun 9, 2017 at 6:25
  • 5
    Please people stop using jQuery AND Angular together. Either you do things the jQuery way, OR the Angular way, not both. Commented Jun 9, 2017 at 6:25
  • @JeremyThille is there any way to change css on runtime using angular js? Commented Jun 9, 2017 at 6:28
  • Of course there is. Everything you can do with jQuery, you can also do with Angular. But the Angular way :) Documentation : ngStyle Commented Jun 9, 2017 at 6:33

3 Answers 3

1

Better not to use jquery with angular

Here is an angular way to achieve your requirement

Since you are trying to make every first child image as opacity 0.5 the loop always makes every images as first child/

So, you can use ng-style in the image div to apply the opacity.

ng-style="{'opacity': '0.5'}"

<div>
    <span ng-repeat="image in post.postImages" ng-if="$index <= 3" ng-init="image.showDeleteIcon= false" ng-mouseover="image.showDeleteIcon=true" ng-mouseleave="image.showDeleteIcon=false">
        <span ng-if="$index == 3" ng-init="setImgCounter(post.postImages.length,$parent.$index+''+$index)"></span>
        <a id="{{$parent.$index}}{{$index}}" onclick="SetDataGallery(this.id)" ng-href="../upload/post-photos/{{image.attachmentURL}}">
            <img class="img-responsive feed-photo" ng-src="../upload/post-photos/{{image.attachmentURL}}" alt="Photo" style="display:inline;" ng-style="{ 'opacity' : ($index == 3) ? '0.5' : '1' }">
            <a href="#" class="imgDelete" ng-if="post.timelineStrore.hasControl" ng-show="image.showDeleteIcon" title="Delete Photo" ng-click="DeletePostAttachment(post.timelineStrore.id, image.postAttachmentId,'image')">
                <i class="fa fa-close"></i>
            </a>
        </a>
    </span>
</div>

Since the image is the first-child to anchor, it will only apply to image..but not i tag.

Note: If you wanted to apply it only for 3rd index, you can also use condition in ng-style. Angular is so flexible.

ng-style="{ 'opacity' : ($index == 3) ? '0.5' : '1' }"

I think this is your requirement.. pls ask me if any queries.

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

3 Comments

cant use because i only need to add opacity when the $index = 3.
you can use, check this and updated answer , ng-style="{ 'opacity' : ($index == 3) ? '0.5' : '1' }"
Glad to help you. :)
1

Use ng-class for dynamically changing css using angularjs. This will allow you to dynamically set classes.

I assume you have your classes defined in a style-sheet that is loaded into memory.

.white{
    color: #ffffff;
}
.black{
    color: #000000;
}

In your angular controller you can have a variable defined that will hold your class-name. Here I am setting it to apply 'white' by default.

$scope.myClass = "white";

Then in your markup you simply bind that variable to your element with ng-class.

<div ng-class="myClass">....</div>

Now, whenever $scope.myclass changes the appropriate class will be added to the div and the old class will be removed. So, in your controller you'll have something that will trigger a change...

if( some_condition ){
    $scope.myClass = "black";
} else {
    $scope.myClass = "white";
}

Comments

0

If there is possibility with angular then don't use JQuery

Do this instead and make sure document.querySelectorAll("#" + id + " img:first-child") this is getting correct element

$scope.setImgCounter = function (counter,id)
{
    angular.element(document.querySelectorAll("#" + id + " img:first-child")).css("opacity", "0.5");
}

4 Comments

That is why I said "make sure document.querySelectorAll("#" + id + " img:first-child") is getting correct element". Where you are calling setImgCounter ?
after inspaction ` <a id="33" onclick="SetDataGallery(this.id)" ng-href="../upload/post-photos/16b179d6-6644-484c-b6b1-7e896a68242c.jpeg" href="../upload/post-photos/16b179d6-6644-484c-b6b1-7e896a68242c.jpeg"> <img class="img-responsive feed-photo" ng-src="../upload/post-photos/16b179d6-6644-484c-b6b1-7e896a68242c.jpeg" alt="Photo" style="display:inline;" src="../upload/post-photos/16b179d6-6644-484c-b6b1-7e896a68242c.jpeg"> </a> `
I didn't get you. You mean that is the element you are getting in the console?
copied from inspaction

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.