我想在 ng 模型中获取 ng 值的值,以便我可以在控制器部分使用数据.是否有可能或有任何其他方法



网页格式的代码

<div ng-repeat="cityobj in cityData" class="tabledata">
           <input type="text" ng-value="{{cityobj.temp.humidity}}" ng-model="humidity">
            <div>

在控制器中

console.log("The value in textfield is",$scope.humidity);

它总是给出未定义的输出

你不需要ng-value,因为你已经在使用ng-model

<div ng-repeat="cityobj in cityData" class="tabledata">
       <input type="text ng-model="cityobj.temp.humidity">
 <div>

然后它应该工作

console.log("The value in textfield is",$scope.cityData[passindex].cityobj.temp.humidity");

演示

var app =angular.module('testApp',[]);
app.controller('testCtrl',function($scope){
$scope.cityData = [{
  "city": "Klimovsk",
  "temp": {
     "humidity":2
  }
}, {
  "city": "Shalakusha",
   "temp": {
     "humidity":5
  }
}];
$scope.print =function(cityObj){
 console.log(cityObj.temp.humidity);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="testApp" ng-controller="testCtrl">
<div ng-repeat="cityobj in cityData" class="tabledata">
       <input type="text" ng-model="cityobj.temp.humidity">
       <button ng-click=print(cityobj)>PRINT</button>
 <div>
 </body>

最新更新