在 ng 重复时在控制器中获取模型值

getting model value in controller when in ng repeat

我写了如下代码,

<div ng-repeat="rnewobj in rnewobjs">
    <select   name="abc" 
    ng-model="rnewobj.objkey.value" 
    ng-options="item.id as item.name for item in session_dd"
    ng-change="valuechanged()" required>
    </select>

如何获取更改时的模型值。我想从控制器更改模型值。我该怎么做?

你需要实现watcher功能

$scope.$watch('rnewobj.objkey.value', function(data) {
    console.log(data);
});

该值存储在数据变量中。

使用 $scope 个同名变量。它依赖于 Angular 的双向数据绑定。 假设您想编辑数组中的对象索引 2:

app.controller('MyController', function ($scope) {
  var index = 2;
  $scope.rnewobjs[index].objkey.value = 'YourValue';
});

该值将根据您的视图自动更改。

这是您的要求示例,设置值并在更改时打印它(我只是使用了一个按钮,但您可以做任何事情):

http://jsfiddle.net/U3pVM/19572/

HTML

<div ng-repeat="rnewobj in rnewobjs">
    <select
        name="abc"
        ng-model="rnewobj.objkey.value"
        ng-options="item.id as item.name for item in session_dd"
        ng-change="valuechanged(rnewobj)" required>
    </select>
    <button type="button" ng-click="reset(rnewobj)">Reset to item 1</button>
</div>

Javascript

$scope.session_dd = [{
    id: 1,
    name: "item1"
}, {
    id: 2,
    name: "item2"
}, {
    id: 3,
    name: "item3"
}];

$scope.rnewobjs = [{
    objkey: {
        value: 1
    }
}, {
    objkey: {
        value: 2
    }
}, {
    objkey: {
        value: 3
    }
}];

// print the value when changed
$scope.valuechanged = function (item) {
    console.log(item.objkey.value);
};

// set the value
$scope.reset = function (item) {
    item.objkey.value = 1; // reset to 1
};