While it may be tempting to take a shortcut and ignore MVC principles, I am determined to learn how to work harmoniously with Angular. I have been struggling with seemingly trivial issues, such as this one.
My goal is for div#thisShouldFade
to smoothly fade out when a new option is selected, then fade back in with the updated data.
Below is the HTML code:
<body ng-app="MyApp">
<div ng-controller="MyController as myCtrl">
<select ng-model="myCtrl.currentThing" ng-options="object.name for object in myCtrl.things">
<option value="">--choose a thing--</option>
</select>
<div id="thisShouldFade">{{myCtrl.currentThing.data}}</div>
</div>
</body>
And here's the JavaScript code:
angular.module("MyApp", [])
.controller("MyController", function(){
this.things = [
{ name: "Thing One", data: "This is the data for thing one" },
{ name: "Thing Two", data: "This is the data for thing two" },
{ name: "Thing Three", data: "This is the data for thing three" }
];
this.currentThing = null;
})
For reference, here's the Plunk link:
http://plnkr.co/edit/RMgEOd6nrT9lFQlslDR0?p=preview
I've experimented with various methods using ngAnimate to apply classes with CSS transitions, but it seems that the model changes too quickly due to being bound to the SELECT
.
If anyone has an effective Angular-based approach, I would greatly appreciate your advice. I would prefer not to rely on jQuery for this task. However, you can see a jQuery example demonstrating the desired effect here: