How to catch the value of an ul / ul with ANGULARJS?

0

I have the following problem when getting the <li> selected from a list <ul> .

I thought it would be the same as in a select that only the ng-model is placed and already, but this is different.

<li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" role="button">
        Categorias <span class="caret"></span>  
    </a>
    <ul class="dropdown-menu" role="menu" >
        <li ng-repeat="objeto in objeto" value="{{objeto.idCategoria}}" ng-model="categoriaElejida" title="{{objeto.idCategoria}}"><a href="#!tecnologia">{{objeto.nombre}}</a></li>
    </ul>
</li>
    
asked by Jose Manuel 10.07.2017 в 03:07
source

1 answer

1

In this case you can not do something as simple as using ng-model , but you can use an alternative to know what category the user has selected using ng-click in this way:

(Keep in mind that you modify the HTML and omit some attributes that you had put for more brevity but the most important thing is this part that adds: ng-click="ctrl.seleccionarCategoria(objeto.idCategoria)" )

<li class="dropdown">
    <a class="dropdown-toggle" data-toggle="dropdown" role="button">
        Categorias <span class="caret"></span>  
    </a>
    <ul class="dropdown-menu" role="menu" >
        <li ng-repeat="objeto in objetos">
            <a href="#!tecnologia"
               ng-click="ctrl.seleccionarCategoria(objeto.idCategoria)">
             {{objeto.nombre}}
            </a>
         </li>
    </ul>
</li>

In the controller it would be something like this:

//...
function seleccionarCategoria(idCategoria) {
    ctrl.idCategoria = idCategoria;
}
//...

In this way, you could carry out any further processing taking into account the id of the last category that was selected. Pr example, like this:

//...
function mostrarCategoria() {
    console.log("el id de la categoria es: " + ctrl.idCategoria);
}
//...
    
answered by 10.07.2017 в 17:20