Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在AngularJS中调整句柄元素大小_Javascript_Jquery_Angularjs_Angularjs Directive - Fatal编程技术网

Javascript 在AngularJS中调整句柄元素大小

Javascript 在AngularJS中调整句柄元素大小,javascript,jquery,angularjs,angularjs-directive,Javascript,Jquery,Angularjs,Angularjs Directive,我有一个有两个div的指令: <div id="childContainer"> <div id="child"> <a class="btn btn-primary testBtn1" ng-show="!vm.hideButton">Test Button 1</a> <a class="btn btn-secondary testBtn2">Test Button 2</a> </div&

我有一个有两个div的指令:

<div id="childContainer">
  <div id="child">
    <a class="btn btn-primary testBtn1" ng-show="!vm.hideButton">Test Button 1</a>
    <a class="btn btn-secondary testBtn2">Test Button 2</a>
  </div>
</div>

然而,这并不像预期的那样有效。好像手表功能触发得太晚了。您可以查看该问题。

$watch
应该接受您要监视的变量,而不是值

return vm.child.height(); //vm.child is a jQuery object
在这里,您将返回
vm.child.height()的值。改为:

$scope.$watch('vm.child.height()', function(){
  vm.childContainer.height(vm.child.height()); //vm.childContainer is a jQuery object
});

将手表更改为以下内容:
$scope.$watch('vm.child.height',function(){vm.childContainer.height(vm.child.height());})
.Hmm。我试过了,但是手表没有触发。也许这和我用的是controllerAs有关?是的,给我一点时间。我刚刚意识到你在函数的返回值上加了一块手表--
$watch
不是那样工作的。你把手表放在一个变量上,而不是一个值上。我会把我的评论转发给你。。我试过了,但是没有触发。我还试着看vm.child.offsetHeight,但没有成功。@Brandon这与我的评论不同。哦,对不起。我真不敢相信你能成功。一天多来,我一直试图回避这个问题。谢谢你新鲜的眼睛总是有帮助的。确保在
$watch
语句中使用字符串。它更容易阅读,而且我99%确定它应该是字符串。
$scope.$watch('vm.child.height()', function(){
  vm.childContainer.height(vm.child.height()); //vm.childContainer is a jQuery object
});