简体   繁体   English

在AngularJS指令中使用单向绑定

[英]Using One-Way Binding in AngularJS Directive

I have a directive defined like this: 我有这样定义的指令:

myApp.directive('stoplight', function() {
    return {
        restrict:'E',
        transclude: true,
        scope: {
            value: '@'
        }, 
        link: function(scope, element) {
            if (scope.value === true) {
                element.html('<i class="icon icon-true green"></i>');
            } else if (scope.value === false) {
                element.html('<i class="icon icon-false red"></i>');
            } else {
                element.html('<i class="icon icon-unknown yellow"></i>');
            }
        }
    };
});

When I use this directive, I use the following markup: 使用此指令时,将使用以下标记:

<stoplight value="boolValue" />

My controller behind stoplight looks like this: 我的红绿灯后面的控制器如下所示:

myApp.controller('MyController', function($scope, $http) {
    $scope.boolValue = null;
    $scope.init = function() {
      $scope.boolValue = false;
      $http.jsonp('http://anyorigin.com/get?url=www.google.com&callback=JSON_CALLBACK')
        .success(function() {
            console.log('woohoo!');
            $scope.boolValue = true;
        });
    };

    $scope.init();
}};

I have two issues, and neither make sense to me. 我有两个问题,对我来说都没有意义。

  1. The ' @ ' in my directive doesn't work. 我的指令中的“ @ ”无效。 If I change the ' @ ' to a ' = ', the link function works somewhat as expected. 如果我将' @ '更改为' = ',则链接功能会按预期工作。 However, I want to use one-way binding instead of two-binding for performance reasons. 但是,出于性能原因,我想使用单向绑定而不是两个绑定。
  2. For some reason, the $scope.boolValue = true; 由于某种原因, $scope.boolValue = true; in my success callback doesn't update the UI. 在我的成功回调中不会更新UI。 The icon stays red. 图标保持红色。 I can set the value to null, expecting yellow, but it stays red. 我可以将值设置为null,期望为黄色,但仍保持红色。 If I look in the console window though, I can see ' woohoo! 但是,如果我在控制台窗口中查看,我会看到“ woohoo! '. ”。 I don't understand why updating boolValue outside of the callback works, yet in the callback, it just doesn't work. 我不明白为什么在回调之外更新boolValue可以工作,但是在回调中却不起作用。 I do not see any error in the console window or anything of that nature. 我没有在控制台窗口中看到任何错误或任何类似的性质。

Can someone please help me identify why this isn't working? 有人可以帮我确定为什么这不起作用吗? I'm not sure if this is one issue or two issues. 我不确定这是一两个问题。 I think they both have to do with the binding. 我认为它们都与绑定有关。 However, I'm not sure how to remedy this. 但是,我不确定该如何解决。

Thank you. 谢谢。

Regarding issue #1, I believe it is occurring because '@' always results in a string value. 关于问题#1,我相信是因为“ @”总是导致字符串值而发生。 So you probably need scope.value === 'true' and scope.value === 'false'. 因此,您可能需要scope.value ==='true'和scope.value ==='false'。

Regarding issue #2, as neilhem already answered, you need the double curly braces: 关于问题2,正如neilhem已经回答的那样,您需要双花括号:

<stoplight value="{{boolValue}}" />

使用大括号<stoplight value="{{ boolValue }}" />

For #1: using @ doesn't mean 'one-way binding', it means to take evaluated value of the DOM attribute, not the variable, so scope.value will result in a string value of 'boolValue', you could use <stoplight value="{{ boolValue }}" /> in your template or = in your directive. 对于#1:使用@并不意味着“单向绑定”,这意味着要获取DOM属性的评估值,而不是变量,因此scope.value将导致字符串值“ boolValue”,您可以使用模板中的<stoplight value="{{ boolValue }}" />或指令中的= Here is a great explanation how @ and = differs. 这是一个很好的解释, @=有何不同。

For #2: Your UI doesn't update, because you directive is not watching for changes on attribute value. 对于#2:您的UI不会更新,因为您的指令没有监视属性值的更改。 When directive is initialized, it takes first value and does the stuff in your directive, however - that's it. 初始化指令后,它将获得第一个值,并执行指令中的操作,仅此而已。 When value changes, nothing should happen in directive. 当值改变时,指令中什么也不会发生。 Try this: 尝试这个:

myApp.directive('stoplight', function() {
  return {
    restrict:'E',
    transclude: true,
    scope: {
      value: '@'
    },
    link: function(scope, element) {
      scope.$watch(function() { return scope.value; }, function(value) {
        if (value === true) {
          element.html('<i class="icon icon-true green"></i>');
        } else if (value === false) {
          element.html('<i class="icon icon-false red"></i>');
        } else {
          element.html('<i class="icon icon-unknown yellow"></i>');
        }
      });
    }
  };
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM