Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change div content dynamically on other div click (AngularJS)

So, I'm new to AngularJS and I'm trying to change a div content after another is clicked (this one holds a div with the content that I want to put on the first one).

HTML

<div ng-controller="dCtrl">
    <ul ng-repeat="product in products">
        <li change>
            {{product.name}}
            <div class="hide">{{product.description}}</div>
        </li>
    </ul>
</div>

<div id="test"></div>

Javascript

var app = angular.module("dt", []);

app.directive("change", function() {

    return function(scope, element) {

        element.bind("click", function() {
           var message = element.children("div").text();
           console.log("breakpoint");

           angular.bind("#test", function() {
               this.text(message);
           });
        })
    }
})

app.controller("dCtrl", function($scope) {

$scope.products = [
    { "name" : "Escova XPTO", "description": "Lava tudo num instante"},
    { "name" : "Pasta de Dentes YMZ", "description": "Dentifrico do camandro"}
];

})

I know that I could just say:

$("#test").html(message);

But I'm still confused about mixing jQuery and AngularJS, I dont know if that is a correct way of doing it

Thanks

like image 403
Bruno Teixeira Avatar asked Apr 16 '13 17:04

Bruno Teixeira


1 Answers

Setup ng-click:

ngClick is for doing things such as the scary jQuery-esque stuff you have going on in your change directive. Place ng-click in your clickable div's attributes and pass in a method that changes the $scope variable accepted by...

ngShow and ngHide.

When true these directives, as the name states, show or hide the associated html object. You can pass in $scope variables determine the boolean value. When the $scope updates these methods automatically update the DOM to show/hide the element.

like image 178
Brian Petro Avatar answered Oct 14 '22 14:10

Brian Petro