Angular Custom指令更改属性值

时间:2021-11-23 20:34:27

Struggling to find out the best way to watch for attribute changes, which would ideally update based on a keypress event, bound to the scope in the parent controller

努力寻找观察属性变化的最佳方式,理想情况是基于keypress事件更新,绑定到父控制器中的范围

I would like each 'instance' of the directive to have its own 'hasFocus' property to be changed by updating the attribute value e.g.

我希望指令的每个'实例'都有自己的'hasFocus'属性,可以通过更新属性值来改变它。

<menu has-focus="{{ true }}" ></menu>
<menu has-focus="{{ false }}" ></menu>
<menu has-focus="{{ false }}" ></menu>

template:

<div class="menu">
<ul>
    <li ng-repeat="option in menu.options" ng-class="{ 'focused' : hasFocus  }" tabindex="{{ $index }}">
        <div class="icon">{{ $index+1 }}</div>
    </li>
</ul>

and so only 1 directive can have the value equal to 'true' at any one time.

因此,任何时候只有1个指令的值可以等于'true'。

I have a custom directive

我有一个自定义指令

.directive('menu', function()
{
  restrict : 'E',
  templateUrl : 'partials/menu-left.html',
  replace : true,
  scope : {
        hasFocus : '@'
  },
  link : function( $scope, element, attrs )
  {
    attrs.$observe('hasFocus', function(value) {
        console.log(value);
     });
  }

}) 

but cant seem to extract the value from the $observe method tried using $watch but still didnt work not sure what I am doing wrong!

但似乎无法从使用$ watch尝试的$ observe方法中提取值,但仍然无法确定我做错了什么!

1 个解决方案

#1


2  

if you use the @ binding, you might have to use $watch like this:

如果你使用@绑定,你可能必须使用$ watch这样:

$scope.$watch(function(){
   return attrs.hasFocus;
}, function(val) {
     $scope.hasFocus = val;
});

if that doesn't work, or if you prefer two-way binding with =:

如果这不起作用,或者你更喜欢双向绑定=:

<menu has-focus="true" ></menu>

and

.directive('menu', function()
{
  restrict : 'E',
  templateUrl : 'partials/menu-left.html',
  replace : true,
  scope : {
        hasFocus : '='
  },
  link : function( $scope, element, attrs )
  {
    // $scope.hasFocus holds true/false
  }

}) 

I think two-way binding is better especially with booleans because if you only need it to control how the DOM looks, you might not even need to watch for a change, you'd just have to plug in $scope.hasFocus into the DOM somewhere (ng-show, ng-switch, etc.)

我认为双向绑定更好,特别是对于布尔值,因为如果你只需要它来控制DOM的外观,你可能甚至不需要注意改变,你只需要将$ scope.hasFocus插入到DOM中某处(ng-show,ng-switch等)

EDIT: yes, I just noticed your template, so if you use two-way binding (=) you don't need the watch

编辑:是的,我刚注意到你的模板,所以如果你使用双向绑定(=)你不需要手表

#1


2  

if you use the @ binding, you might have to use $watch like this:

如果你使用@绑定,你可能必须使用$ watch这样:

$scope.$watch(function(){
   return attrs.hasFocus;
}, function(val) {
     $scope.hasFocus = val;
});

if that doesn't work, or if you prefer two-way binding with =:

如果这不起作用,或者你更喜欢双向绑定=:

<menu has-focus="true" ></menu>

and

.directive('menu', function()
{
  restrict : 'E',
  templateUrl : 'partials/menu-left.html',
  replace : true,
  scope : {
        hasFocus : '='
  },
  link : function( $scope, element, attrs )
  {
    // $scope.hasFocus holds true/false
  }

}) 

I think two-way binding is better especially with booleans because if you only need it to control how the DOM looks, you might not even need to watch for a change, you'd just have to plug in $scope.hasFocus into the DOM somewhere (ng-show, ng-switch, etc.)

我认为双向绑定更好,特别是对于布尔值,因为如果你只需要它来控制DOM的外观,你可能甚至不需要注意改变,你只需要将$ scope.hasFocus插入到DOM中某处(ng-show,ng-switch等)

EDIT: yes, I just noticed your template, so if you use two-way binding (=) you don't need the watch

编辑:是的,我刚注意到你的模板,所以如果你使用双向绑定(=)你不需要手表