所以我必须对象范围
$scope.hf_directory = data.features;
$scope.brylanty = data.features;
实际上它们包含相同的数据,但我想知道手表是如何工作的,所以我可以在我的项目中使用它。
我想观看hf_directory,我使用filter:search,因此在输入内容输入后会更新scope.brylanty fiels ng-model =“search”
我试图做这样的事情,但没有做任何改变
$scope.$watch('hf_directory', function (newValue, oldValue, $scope) {
if(newValue) {
$scope.brylanty = newValue;
}
});
两个范围对象都是通过ng-repeat循环显示的,首先我使用过滤器:搜索另一个没有,我知道我可以使用另一个过滤器:搜索,但我想学习如何使用手表;)
我的对象是geojson数据,它们包含相同的值,geojson如下所示:
{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "name": "SomeName"}, "geometry": { "type": "Point", "coordinates": [ 11.410585, 11.293361 ] } },
{ "type": "Feature", "properties": { "name": , "SecondName": { "type": "Point", "coordinates": [ 11.410585, 11.293361 ] } }, .......
]
}
在你的建议之后我尝试了这个
$scope.search = {};
$scope.$watch('search', function (newVal) {
$scope.brylanty = newVal;
});
这个
$scope.$watch('search', function (newValue, oldValue, $scope) {
if (newValue) {
$scope.brylanty = newValue;
}
});
但是没有任何好的结果,在两种情况下,当我开始输入一些物体时,brylanty正在消失?
答案 0 :(得分:0)
如果您想在输入ng-model =“search”中输入内容进行更改,那么理想情况下您应该正在观看搜索变量。它应该是
$scope.$watch('search', function (newValue, oldValue, $scope) {
if(newValue) {
$scope.brylanty = newValue;
}
});
这也可以写成(少于2行)
$scope.$watch('search', function (newValue, oldValue, $scope) {
$scope.brylanty = (newValue) ? newValue : $scope.brylanty;
});
或者你可以在输入字段中使用ng-change并拥有
<input ng-model="search" ng-change="brylanty = (search) ? search : brylanty"/>
观看hf_directory无效,因为在搜索中输入不会影响它。
编辑:如果你真的想看hf_directory并且它是一个对象/数组,那么使用
$scope.$watchCollection()
使用相同的参数
答案 1 :(得分:0)
更新已添加Demo
根据Docs
您可以通过执行类似
的操作来观看任何变量$scope.$watch(function(){
return $scope.hf_directory; // or Directories.hf_directory if Directory is a factory/service
}, function(newVal, oldVal){
if(newVal != oldVal){
$scope.newestValue = newVal; // Do some cool UI
}
}, true); // true to check object equality (see below)
对象相等等同于angular.equal Source
我希望这会有所帮助。