简体   繁体   English

使用大括号在属性中指定角度表达式

[英]Using curly brackets for specifying angular expressions in attributes

I am aware that in Angular its not recommended to use double curly brackets with expressions in attributes but why if they are still used those expressions evaluate in some cases and not in the others.我知道在 Angular 中,不建议在属性中使用带有表达式的双大括号,但是为什么如果仍然使用它们,这些表达式在某些情况下会评估而不是在其他情况下。 Here's the plunker with the given example.这是带有给定示例的plunker The alert box here displays an empty string, but if you move $scope.mytest assignment outside of the service's 'then' function (as shown below) then it works fine.此处的警报框显示一个空字符串,但如果您将 $scope.mytest 分配移到服务的“then”函数之外(如下所示),则它可以正常工作。 Is that a bug or expected behaviour?这是一个错误还是预期的行为?

app.controller('MainCtrl', function($scope, jsonService) {
  $scope.mytest = 'Bzzz';
  jsonService.getjson().then(function(d) {
    $scope.sourceData = d;
    $scope.loaded = true;
  });

});

EDITED:编辑:

The above condition can be observed when an attribute contains more than one expression, which should evaluate to scope variables.当一个属性包含多个表达式时,可以观察到上述条件,这些表达式应该计算为作用域变量。 But if you leave only one expression in the attribute everything works fine :但是如果你只在属性中留下一个表达式,一切正常:

<my-directive myattr="{{mytest}}">{{mytest}}{{mytest}}</my-directive>

even when the scope variable is set within the 'then''s function:即使在“then”的函数中设置了范围变量:

app.controller('MainCtrl', function($scope, jsonService) {
  jsonService.getjson().then(function(d) {
    $scope.mytest = 'Bzzz';
    $scope.sourceData = d;
    $scope.loaded = true;
  });

});

The problem is the double expression won't be reevaluated.问题是不会重新评估双重表达式。 So, in your first example, when the value is assigned directly to the $scope property (ie "at initialization"), your code will work.因此,在您的第一个示例中,当值直接分配给 $scope 属性时(即“在初始化时”),您的代码将起作用。

However, once you move the assignment to the promise handler, the attribute expression won't be notified anymore of the changes.但是,一旦您将分配移至 promise 处理程序,属性表达式将不再收到有关更改的通知。

If you want to make your example work, do so by combining the two expressions into one, like:如果您想让您的示例工作,请将两个表达式合并为一个,例如:

<my-directive myattr="{{mytest + mytest}}">{{mytest}}{{mytest}}</my-directive>

This is not about using {{}} in attributes.这与在属性中使用 {{}} 无关。 Html is view, js is model. Html 是视图,js 是模型。 You should access model from view, you should not access view from model.您应该从视图访问模型,而不应该从模型访问视图。

Html is updated using $watchers, order of $watchers is not defined - your code should not depend on which watcher was run first, so you should not address result of one watcher in another watcher. Html 使用 $watchers 更新,未定义 $watchers 的顺序 - 您的代码不应取决于首先运行哪个观察者,因此您不应在另一个观察者中解决一个观察者的结果。

Had similar situation and simply done with:有类似的情况,只需完成:

<my-directive :myattr="mytest">{{mytest}}</my-directive>

if outside of HTML element (such as in attribute), use version with double curly braces.如果在 HTML 元素之外(例如在属性中),请使用带双花括号的版本。

Another example, with string + expression:另一个例子,用字符串 + 表达式:

<a :href="'mailto:' + contactEmailAddress">{{contactEmailAddress}}</a>

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

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