【问题标题】:Angular Directive using '=' scope & controllerAs使用“=”范围和控制器的角度指令
【发布时间】:2015-07-20 19:39:06
【问题描述】:

我正在尝试编写指令来解码和编码一些信息。基本上,我使用 Int 将 5 条二进制信息存储在数据库中(为每个位赋予 16、8、4、2、1 的值)。

我需要编辑数据的方法:作为 Int 和作为位。但是我在将它们连接起来时遇到了很多问题,并且这些问题(故意)通过使用 controllerAs 表示法来复杂化。

1) 当我更改 Int 时,位没有任何变化。这是因为链接功能似乎只加载一次 - 我怎样才能更改指令中的模型更新信息?

<input ng-model="c.int" type="text" />
<testdir score="c.int"></testdir>

我的指令包括:

scope: {
  score: '='
},
controller: function() {
  var vm = this;
  vm.recChange = function() {
    // convert bits to score
  }
},
controllerAs: 'd',
link: function(scope, elem, attrs) {
  // convert score to bits

还有一个将分数转换为scope.recommendations的链接函数,这是一个5位数组。

2) 当我更改位(并让控制器使用 $scope)时,Int 确实发生了变化。但我找不到使用 controllerAs 的方法 - 链接函数使用 scope 而我的控制器需要 d. 前缀。

template: "<label ng-repeat='(key, recommendation) in recommendations' class='checkbox-inline'>

// template: "<label ng-repeat='(key, recommendation) in d.recommendations' class='checkbox-inline'> \

Plnkr

【问题讨论】:

    标签: angularjs


    【解决方案1】:

    您必须使用scope.score 而不是使用分数的属性值,因为您在分数的范围声明中使用双向数据绑定表示法=。此外,当某个值的更改存在时,只需为该值放置观察者并在其中应用您的更改。

    更新:只需在指令定义的require 键值中添加指令名称,您就可以在链接函数中作为第四个参数访问控制器。

    FORKED PLUNKER

    .directive('testdir', function() {
      var recommendationCategories = ['16RotM', '8Top5', '4Price', '2Cuisine', '1Area'];
    
      return {
    
        require: 'testdir',
    
        scope: {
          score: '='
        },
    
        controller: function () {
           // .....
        },
    
        controllerAs: 'd',
    
        link: function(scope, elem, attrs, ctrl) {
    
          console.log(ctrl);
    
          scope.$watch('score', function() {
            scope.recommendations = {};
            var score = parseInt(scope.score);
            // decode recommendations
            // convert to binary string
            var bitVal = score.toString(2);
            //pre-pad with "0"
            var e = recommendationCategories.length - bitVal.length;
            bitVal = "0".repeat(e) + bitVal;
    
            recommendationCategories.forEach(function (cat, idx) {
              scope.recommendations[cat] = {checked: (bitVal[idx]=="1") ? true : false};
            });
    
            // add a field for closed too
            scope.recommendations.closed = {checked : (score < 0)};
          });
        },
        template: "<label ng-repeat='(key, recommendation) in recommendations' class='checkbox-inline'> \
                    <input type='checkbox' ng-model='recommendation.checked' ng-change='d.recChange()'/>{{key}} \
          -        </label>"
        // template: "<label ng-repeat='(key, recommendation) in d.recommendations' class='checkbox-inline'> \
      }
    
    });
    

    【讨论】:

    • 很好 - 解决了第 1 部分,但留下了问题的第 2 部分
    • 实际上在 $compile 文档中
    • 对不起,但我仍在努力获取编码值 vm.score,退出指令并进入父 plnkr.co/edit/PEWAzuPGJJleQqKW21Ds?p=preview
    • ...除非我使用$scope,我正试图弃用它
    【解决方案2】:

    在您的“controllerAs”值之后,您需要添加 bindToController:true

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多