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

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

问题描述

我有一个模式,我想使用两次,但值不同.例如:

 .directive("day", function($scope){返回 {模板:'<div>{{day}}</div>'}}

我可以在另外两个指令中使用它,但封装day"以查看这样的结果:

 

1

<div>2</div>

特别是如果我想保留所有一天"的绑定值

解决方案

你的意思是这样的?

app.directive('day', function(){返回 {范围: {日值:'='},模板:'<div>{{dayValue}}</div>'}})app.directive('outerDirective', function(){返回 {链接:函数($scope){$scope.days = [1,2,3];},模板:'<day day-value="day" ng-repeat="day in days"></day>'}})

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 中封装 $scope的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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