簡體   English   中英

指令中的封裝鏈接和控制器

[英]Encapsulated Link and Controller in Directive

我不會使用其在一個簡單對象中定義的屬性來編程一個靈活的角度指令。

角度:

contentFactory.directive("listViewDir", function ($compile) {

return {
    restrict: "E",
    scope: {
        datasource: '=',
        config: '='
    },
    controller: function ($scope) {
        return $scope.config.controller($scope);
    },
    link: 
        return $scope.config.link(scope, element, attrs);
    }
}

});

自己的配置對象:

contentFactory.controller("indexCtrl", function ($scope) {
$scope.config = oLiftTabs;})

var configurations = [{
    controller: function ($scope) {

        $scope.ButtonClicked = function () {
            alert('Button wurde geklickt!');
        }

        return $scope;
    },
    link: function (scope, element, attrs){
        var template = "... myTemplate ..";

        element.html(template);
        $compile(element.contents())(scope);
    },
}]

雖然我針對控制器的解決方案運行良好,但不適用於鏈接。 有沒有適合我的方法? 是否可以在封裝方法中實現對服務(例如$ compile)的訪問而無需在指令聲明中聲明它?

這是您要達到的目標嗎? 您沒有明確說明該對象的預期生存位置,因此我假設您希望它作為父控制器。 這似乎不是一個好主意,但是如果不了解您的用例,很難說。

演示

html

<body ng-controller="MainCtrl">
    <list-view-dir config="config"></list-view-dir>
</body>

js

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope, $compile) {


  $scope.config = {

    controller: function ($scope) {

        $scope.ButtonClicked = function () {
            alert('Button wurde geklickt!');
        }

        return $scope;
    },

    link: function (scope, element, attrs){
        var template = '<button ng-click="ButtonClicked()" > Alert</button>';

        element.html(template);
        $compile(element.contents())(scope); 
    },

  };


});


app.directive("listViewDir", function(){

  return { 

      restrict: "E",

      scope: {
          datasource: '=',
          config: '='
      },

      controller: function ($scope) {
        return $scope.config.controller($scope);
      },

      link: function(scope, element, attrs){
          return scope.config.link(scope, element, attrs);
      }

  };

});

更新資料

從您的評論看來,您需要使用工廠。 也許是這樣的嗎? 感覺很難看,但這可能正是您想要的。

演示2

var app = angular.module('plunker', []);

app.factory('directiveConfigurations', function($compile){

  var configurations = {

    'listViewDir': {

      controller: function ($scope) {

          $scope.ButtonClicked = function(){
              alert('Button wurde geklickt!');
          };

          return $scope;
      },

      link: function (scope, element, attrs){
          var template = '<button ng-click="ButtonClicked()" > Alert</button>';

          element.html(template);
          $compile(element.contents())(scope);
      }

    }

  };


  return {
    get: get
  };
  ////////////////////////

  function get(key){
    return configurations[key];
  }

});

app.controller('MainCtrl', function($scope, directiveConfigurations) {

  $scope.config = directiveConfigurations.get('listViewDir');

});


app.directive("listViewDir", function(){

  return { 

      restrict: "E",

      scope: {
          datasource: '=',
          config: '='
      },

      controller: function ($scope) {
        return $scope.config.controller($scope);
      },

      link: function(scope, element, attrs){
          return scope.config.link(scope, element, attrs);
      }

  };

});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM