如何在没有ng-model的情况下获得ng-repeat的值?

时间:2017-07-07 13:32:12

标签: angularjs

我使用ng-repeat来迭代对象列表。当用户选择一个选项时,我想要的是该对象,而不是单个值。 ng-model似乎只给了我一个身份。

2 个答案:

答案 0 :(得分:0)

您可以更改您的选择以使用该对象,从而更改模型:

alert(JSON.stringify($scope.subappData.value));

它会显示名称,但它会使用该对象。所以对你来说,vm.subapps(或你控制器中的this.subapps)是一个对象。

编辑:由于您不想显示ID,因此您只需更改提醒行即可:

import RPi.GPIO as GPIO
import time      
sensor_name_0 = "test"
printed_out = False
printed_in = False  

try:
    while True:   
    if sensor_name_0:
        sensor_0 = open('/sys/devices/w1_bus_master1/w1_master_slaves','r').read().split('\n')[0]
        sensor_1 = open('/sys/devices/w1_bus_master1/w1_master_slaves','r').read().split('\n')[1]

        sensorpath = "/sys/bus/w1/devices/"                   
        sensorfile = "/w1_slave"                                          

    def callsensor_0(sensor_0): 
        f = open(sensorpath + sensor_0 + sensorfile, 'r')     
        lines = f.readlines()                                                 
        f.close()                                                                
        temp_line = lines[1].find('t=')
        temp_output = lines[1].strip() [temp_line+2:] 
        temp_celsius = float(temp_output) / 1000          
        return temp_celsius

    def callsensor_1(sensor_1):         
        f = open(sensorpath + sensor_1 + sensorfile, 'r')     
        lines = f.readlines()                                                 
        f.close()                                                                 
        temp_line = lines[1].find('t=')
        temp_output = lines[1].strip() [temp_line+2:]  
        temp_celsius = float(temp_output) / 1000          
        return temp_celsius


    outside = (str('%.1f' % float(callsensor_0(sensor_0))).rstrip('0').rstrip('.'))  
    inside = (str('%.1f' % float(callsensor_1(sensor_1))).rstrip('0').rstrip('.'))  
    print "loop"

    if outside > inside and not printed_out:                         
        printed_out = True
        print "outside is higher then inside"
        print  outside


    if outside < inside and not printed_in:
        printed_in = True
        print "inside is higher then outside"
        print  inside

    time.sleep(5)    

except KeyboardInterrupt:
print('interrupted!')

然后你不会显示ID。此外,不要认为你需要JSON.stringify(),但也建议使用console.log()而不是alert()

答案 1 :(得分:0)

您应该在对象中保存id和值:

HTML:

<div ng-app="myapp" ng-controller="FirstCtrl">
    <select class="form-control selectpicker"  data-live-search="true"   ng-model="subappData"  id="subapplicationid" style="height: 21px;width: 300px;-moz-margin-start: 132px;margin-left: 138px;margin-top: 2px;-moz-margun-start: 132px;">
         <option value="">Select</option>
      <option  ng-repeat='subapp in subapplicationList | filter:query' data-select-watcher data-last="{{$last}}" value = "{{subapp.ID}}"> <a href="#"> {{subapp.ID}} - {{subapp.DESCRIPCION}} </a> </option>
               </select>
    <div>                   
   <div>
    <button  ng-click="whenClick()">Click</button>
   </div>     
    </div>  
</div>

JavaScript的:

var myapp = angular.module('myapp', []);
myapp.controller('FirstCtrl', function ($scope) {
   $scope.subapplicationList = [
  {"ID" :  9 ,"DESCRIPCION" : "a","value" : 24 },
  {"ID" :  1 ,"DESCRIPCION" : "b","value" : 7 },
  {"ID" :  2 ,"DESCRIPCION" : "c","value" : 12 },
  {"ID" :  3 ,"DESCRIPCION" : "d","value" : 2 },
  ];
   $scope.whenClick = function() {
   alert(JSON.stringify($scope.subappData));
  };
});
myapp.directive('selectWatcher', function ($timeout) {
    return {
        link: function (scope, element, attr) {
            var last = attr.last;
            if (last === "true") {
                $timeout(function () {
                    $(element).parent().selectpicker('val', 1);
                    $(element).parent().selectpicker('refresh');

                });
            }
        }
    };
});

工作代码:jsfiddle

相关问题