如何封装在Angular.Js $范围 [英] How to encapsulate $scope in Angular.Js

查看:102
本文介绍了如何封装在Angular.Js $范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模式,我想用了两次,但不同的值。例如:

  .directive(天,函数($范围){
     返回{
        模板:'< D​​IV> {{天}}< / DIV>'
        }
    }

我可以用它在另两个指令,但封装天,看看结果这样的:

 < D​​IV> 1 LT; / DIV>
    < D​​IV> 2'; / DIV>

特别是,如果我希望将所有天

的绑定值
解决方案

你的意思是这样呢?

\r
\r

app.directive('天',函数(){\r
  返回{\r
    范围: {\r
      dayValue:'='\r
    },\r
    模板:'< D​​IV> {{dayValue}}< / DIV>'\r
  }\r
})\r
\r
app.directive('outerDirective',函数(){\r
  返回{\r
    链接:功能($范围){\r
      $ scope.days = [1,2,3];\r
    },\r
    模板:'<日复一日值=天NG重复=日日>< /天>'\r
  }\r
})

\r

\r
\r

I have a pattern, which I want to use twice, but with different values. For example:

 .directive("day", function($scope){
     return {
        template: '<div>{{day}}</div>'
        }
    }

Can I use it in two another directive, but encapsulate 'day' to see result like that:

    <div>1</div>
    <div>2</div>

Especially if I want to keep binding value of all 'day'

解决方案

you meant something like this?

app.directive('day', function(){
  return {
    scope: {
      dayValue: '='
    },
    template: '<div>{{dayValue}}</div>'
  }
})

app.directive('outerDirective', function(){
  return {
    link: function($scope){
      $scope.days = [1,2,3];
    },
    template: '<day day-value="day" ng-repeat="day in days"></day>'
  }
})

这篇关于如何封装在Angular.Js $范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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