Set object in Angular JS

Asked

Viewed 180 times

0

I have the following listing structure:

<div style="position: fixed;z-index: 999;top:20%;margin:0 auto;left:25%;width:50%;padding:5%;background:#FFF;"  ng-if="templateForm == 'light-box-selectcasa'"> 
    <div class="col-md-12">
        <label>SELECIONE UMA CASA PARA LISTAR AS INFORMAÇÕES</label>
        <select class="form-control" ng-options="item as item.referencia for item in casas.list track by item.id" ng-model="obj.casa"></select>
    </div>
    <div class="col-md-12">
        <br/>
        <button class="btn btn-block btn-primary" ng-click="setCasa()">SELECIONAR CASA</button>
    </div>
    <div style="position:fixed;width:1px;height:1px;float:left;"><img style="position:relative;float:left;right:190px;bottom:50px;" src="<?= base_url('assets/img/man-calendar.png') ?>"/></div>
</div>

The listing, is correct, lists the database items and etc.

And then I have the function inside the angular, setCasa():

var casaRef = '';
app.controller("agendaController", function ($scope, $http, $timeout) {

    $scope.setCasa = function () {
        casaRef = $scope.obj.casa;
        $scope.template = '';
        $scope.templateForm = '';
    };
}

But when for the action of setCasa, if I give an Alert in casaRef, it comes only with [Object Object].

How to pass this variable so that when I give the Alert, arrive then what was selected?

  • Put Alert(JSON.stringify(casaRef));

  • Appeared a lot of things now rsrs

  • Is it that or do you want something different?

  • Thus it worked oh Alert(JSON.stringify(casaRef['id'])); only that even so, I do not know how to set the variable casaRef, with this id

  • So I think your ng-model is setting obj.casa with the entire array of options and not with the selected one. Probably ng-options has some problem. I will analyze it

  • This here houseRef = $Scope.obj.casa; has to be set what came from the list..

Show 2 more comments

1 answer

1


Use ng-option as follows to get only the id of the selected house:

ng-options="item.id as item.referencia for item in casas.list"

Or use as follows to obtain the complete object with reference:

ng-options="item as item.referencia for item in casas.list"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.