等效的{{#with}}在角 [英] Equivalent of {{#with}} in angular

查看:134
本文介绍了等效的{{#with}}在角的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是相当于 {{#与}} 在角车把?我想从一个方法调用的结果创建数据的子范围。

What's the equivalent to {{#with}} in handlebars in angular? I'd like to create a sub scope with data from the result of a method call.

例如,我想这将是这样的(这显然是不正确的):

For example, I'd imagine it would be something like this (which obviously isn't correct):

<div ng-with="getData(myData)">{{name}}</div>

使用 myData的作为父范围值,名称属性返回的对象。

With myData as a value on the parent scope and name a property on the returned object.

推荐答案

您可以使自己的

module.directive("ngWith", function() {
    return {
        scope: true,
        link: function(scope, elem, attr) {
            var result = scope.$parent.$eval(attr.ngWith);
            for (var prop in result) {
                if (result.hasOwnProperty(prop)) {
                    scope[prop] = result[prop];
                }
            }
        }
    };
});

在标记

<div ng-with="getData(myData)">{{name}}</div>

里面的NG-与属性对父范围进行评估,在返回值的每个属性将被绑定到该指令所创建的新的子范围,根据同名的前pression。

The expression inside the ng-with attribute is evaluated against the parent scope, each property in the return value will be binded to the new child scope created by the directive, under the same name.

我不认为有这种指令,虽然没什么用。如果你只是想显示的名称属性,只是做 {{的getData(MYDATA的)[名称]}} 或保存功能的作用域变量的结果

I don't think there is much use for this directive though. If you just want to display the name property, just do {{getData(myData)['name']}} or save the result of the function to a scope variable

这篇关于等效的{{#with}}在角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆