Loki may not be as bad as a villain as we thought. Trickster's god may not have been in full control when he attacked the Earth at first Avengers movie. Let's go back to the first one Thor. In the movie, Locke learns that half ice-giant has been adopted. His anger and pain forced him to succeed the throne, which should belong to Thor. After a failed attempt to take over, Loki falls from Bifrost and apparently dies. Then, he reappears Avengers after falling from Bifrost to another dimension where he meets the Other and Chitauri.

This is also where Loki and Thanos meet and give Loki the Chitaur army and the scepter of the mind (this happens outside the screen). Marvel's official website updated that particular part of the MCU's canon to include the Mind Stone secretly insulting Loki's will for Tanos intentions all the time. An excerpt from their page reads:

"Arriving at the Sanctuary through the red bush caused by Bifrost, Loki met the Other, ruler of the ancient race of the aliens of Chitauri and Thanos. By offering the Goddess of the Preaching Authority over the favored kingdom of the Earth on Earth, Thanos requested Tesserak in return Nadal with a skipper acted as a device for controlling the mind, Loki could influence others. Without knowledge, the Skeptic also influenced him, prompting his hatred for his brother Thor and the inhabitants of the earth. "

This will explain Loki's loyal loyalty, and his confusion in the first Avengers film, where he says: "I remember the shadow, I live in the shadow of your greatness. I remember that I was caught in the abyss, who was and should have been king." Obviously, The Smart Stone told Loki that Thor stole the throne and threw it into space, which is not what happened.