带过滤器的覆盖值Angularjs ngInit [英] Angularjs ngInit with filter override values

查看:75
本文介绍了带过滤器的覆盖值Angularjs ngInit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想初始化没有成功滤波值。我将使用部分/ NG-包括使变量的名称不能改变。

I trying to init a filtered value without success. I'll use partial/ng-include so variable's name can't change.

我使用angularjs V1.0.2,我也试图与V1.1.1

I using angularjs v1.0.2 and I tried also with v1.1.1

<script>
    function Ctrl($scope) {
      $scope.trains = [ 1, 2];
    }
</script>


<div ng-app ng-controller="Ctrl">
  <div ng-init="t = (trains | filter:1)">    
    {{ t }}
    {{ trains | filter:1 }}
  </div>
  <div ng-init="t = (trains | filter:2)">
    {{ t }}
    {{ trains | filter:2 }}
  </div>
</div>

输出

[2] [1]
[2] [2]

下面是这个例子 - > http://jsfiddle.net/9q5D4/4/

Here is this example -> http://jsfiddle.net/9q5D4/4/

推荐答案

不知道这是你想要的...
但是你可以以NG-init和组合之间使用指令NG-包括:

Not sure this is what you want... But you can use directive in order to combine between ng-init and ng-include:

HTML

<div ng-app="myApp" ng-controller="Ctrl">    
    <my-directive field-name=1 train=2></my-directive>
    <my-directive field-name=2 train=17></my-directive>
</div>

JS:

function Ctrl($scope, $filter) {
  $scope.trains = [1, 2]; 
}

angular.module("myApp", [])
.directive("myDirective", function() {
    return {
        restrict: "E",
        scope: 'isolate',
        template: "<div>{{exposeAttribute}} {{anotherOne}}</div>",
        controller: Ctrl,
        //templateUrl: "mytemplate.html",
        link: function(scope, element, attr) {
            scope.exposeAttribute = attr.fieldName;
            scope.anotherOne = attr.train;
        }
    };
});

和的输出是:

1 2

2月17日

http://jsfiddle.net/9q5D4/5/

这篇关于带过滤器的覆盖值Angularjs ngInit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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