如何封装在Angular.Js $范围装在、范围、Angular、Js

2023-09-14 00:27:41 作者:土豪中的战斗豪!

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

  .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\rapp.directive('outerDirective',函数(){\r  返回{\r    链接:功能($范围){\r      $ scope.days = [1,2,3];\r    },\r    模板:'<日复一日值=天NG重复=日日>< /天>'\r  }\r}) 9 种改善 AngularJS 性能的方法

\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>'
  }
})