2013-04-21 8 views
19

Làm thế nào để chỉ thị có thể gọi một hàm từ bộ điều khiển với một số tham số?Gọi chức năng điều khiển từ chỉ thị với các tham số

Tôi sẽ đưa biến myVar vào phạm vi. $ Apply (attrs.whattodo);

HTML:

<div ng-app="component"> 
    <div ng-controller="ctrl"> 
    <span ng-repeat="i in myarray"> 
    <span customattr whattodo="addVal">{{i}}</span> 
    </span> 
    </div> 

khiển JS:

function ctrl($scope) { 
     $scope.myarray = [1]; 
     $scope.addVal = function (value) { 
      $scope.myarray.push(value); 
     } 
    } 

Chỉ JS:

angular.module('component', []).directive('customattr', function() { 
    return { 
     restrict: 'A', 
     link: function (scope, element, attrs) { 
      var myVar = 5; 
      scope.$apply(attrs.whattodo); 
     } 
    }; 
}); 

Trả lời

17

Đây là một trong những phương pháp làm việc:

Bạn cần phải ràng buộc thuộc tính này trong phạm vi như một mô hình phạm vi với loại chức năng. Vì vậy, bạn có thể thực hiện điều này khi bạn cần trong khác (chỉ thị) sope

HTML

<body ng-controller="MainCtrl"> 
    Value: {{value}}! 

    <button customattr whattodo="addValue">Add</button> 
</body> 

JS

angular.module('component', []) 

.controller('MainCtrl', function($scope) { 
    $scope.value = 1; 

    $scope.addValue = function(val){ 
    alert(val); 
    $scope.value = val; 
    } 
}); 

.directive('customattr', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      whattodo: "=" // or ' someOtherScopeName: "=whattodo" ' 
     }, 
     link: function (scope, element, attrs) { 
      var myVar = 5; 
      scope.whattodo(myVar); // or ' scope.someOtherScopeName(myVar) ' 
     } 
    }; 
}); 

Đây là code on plunker

fromAngularJS: Directives

= or =attr - set up bi-directional binding between a local scope property and the parent scope property of name defined via the value of the attr attribute. If no attr name is specified then the attribute name is assumed to be the same as the local name. Given and widget definition of scope: { localModel:'=myAttr' }, then widget scope property localModel will reflect the value of parentModel on the parent scope. Any changes to parentModel will be reflected in localModel and any changes in localModel will reflect in parentModel

+0

Điều đó hoạt động rất tốt. Một điều cuối cùng: ví dụ tôi đưa ra là một ví dụ dumy. Nhưng với ví dụ "thực" của tôi: giá trị là một danh sách các dicts mà tôi render với một số văn bản đầu vào. Nếu tôi đặt phạm vi trống hoặc phạm vi bạn đã cung cấp, tất cả các trường nhập của tôi sẽ trống. Bất kỳ ý tưởng tại sao?Cảm ơn bạn – JohnCastle

+0

Thực sự khó tưởng tượng ra nó, sẽ không phức tạp để bạn di chuyển eample thực của bạn thành plunker? Cảm ơn – Maksym

+0

Tất nhiên! Đây là: http://plnkr.co/edit/mu9DM1dEyZ36UqVvw0DM Chỉ cần đặt phạm vi thành true trong chỉ thị và bạn sẽ thấy "Toto" trong đó. Hoặc – JohnCastle

10

trong html

whattodo="addVal(value)" 

trong chỉ thị

scope.$apply(function(s){ 
    s.whattodo({value : myVar}); 
}); 
+0

tôi nhận: Lỗi Loại: Sở hữu 'whattodo' của đối tượng # không phải là một chức năng? – JohnCastle

+0

xin lỗi. Tôi sửa nó rồi. lẽ ra phải là s.whattodo – Ketan

+0

Rất thú vị và hoạt động tốt ([demo] (http://jsfiddle.net/BinaryMuse/rhXDs/)). Sau một vài suy nghĩ, điều tương tự phải được sử dụng trong những thứ như '

2

Tại sao bạn không sử dụng "&" đăng nhập vào phạm vi phân lập?

<body ng-controller="MainCtrl"> 
    Value: {{value}}! 
    <button customattr add-val="addValue(value)">Add</button> 
</body> 

Trong điều khiển:

function ctrl($scope) { 
     $scope.myarray = [1]; 
     $scope.addValue = function (value) { 
      $scope.myarray.push(value); 
     } 
    } 

Và trong chỉ thị:

angular.module('component', []).directive('customattr', function() { 
    return { 
     restrict: 'A', 
     scope: { 
      addVal: "&" 
     }, 
     controller: function ($scope) { 
      var myVar = 5; 
      // To execute addVal in controller with 'value' param 
      $scope.addVal({value: value}) 
     } 
    }; 
}); 
+1

trong chỉ thị nơi bạn đã từng sử dụng addVal, thay vào đó hãy sử dụng whattodo – userRaj