0

如果我有 $scope.data={"user" {"firstname":test"}},我希望有类似的模板

{% with data %} 用户名为 {{ user.firstname }}

或者

{% with data.user %} 用户名为 {{ firstname }}

类似的东西,但我找不到它。我认为总是使用像 {{ data.user.firstname }} 这样的完整路径并不理想

任何想法?

4

1 回答 1

1

据我所知AngularJS不支持,那就是python。我认为访问没有问题:

{{ data.user.firstname }} 

在您的模板中。如果你不喜欢那个“数据”。你为什么不分配:

let data={ user: { firstname: "test"} };   
$scope.user = data.user;

然后你就可以在你的模板中使用:

{{ user.firstname }}

您可以使用的另一件事是指令 - 设置您自己的自定义指令并通过绑定传递您想要的值,该绑定将为您提供所需的 $scope 属性。因此,在 HTML 中,您可以执行以下操作:

<some-directive user="data.user" />

然后在指令中(假设您将模块存储在变量中app):

app.directive('someDirective', function() {
  return {
    scope: {
      user: '='
    },
    template: 'Name: {{user.firstname}}'
  };
});

有关指令的更多信息:https ://docs.angularjs.org/guide/directive

于 2017-10-01T17:00:29.203 回答