如何在angularJs中访问控制器到Directive的值

时间:2016-02-29 11:41:18

标签: javascript jquery angularjs html5 css3

我在Controller中获得了值。我希望这些值可以在diective内部访问。有可能..?

$scope.gridheader = [
{ headerName: "ID", field: "ID", seqNo: 0,checkboxSelection: true },
{ headerName: "Patient Name", field: "PatientName", seqNo: 1 },
{ headerName: "Gender", field: "Gender", seqNo: 3 },
{ headerName: "Age", field: "Age", seqNo: 2 },
{ headerName: "Phone Number", field: "mobileNumber", seqNo: 4 }
    ];

$scope.rowData = [
                     { ID: "09-14-002880", PatientName: "PRAVEEN KUMAR", Gender: "Male", Age: "20", mobileNumber: 9879878971, patientId: "test" },
                     { ID: "09-13-000188", PatientName: "VAR", Gender: "Male", Age: "20", mobileNumber: '', patientId: "ZXC12" },
                     { ID: "09-05-019825", PatientName: "KARMA", Gender: "Male", Age: "29", mobileNumber: '', patientId: "ZA2545635" },
                     { ID: "09-04-010524", PatientName: "FRANKLIN ANTHONY", Gender: "Male", Age: "20", mobileNumber: '', patientId: "Z7552396" },
                     { ID: "09-08-009303", PatientName: "DARYOUSH", Gender: "Male", Age: "29", mobileNumber: '', patientId: "Z2548467" },
                     { ID: "09-12-031048", PatientName: "SMITA", Gender: "Female", Age: "20", mobileNumber: 9880222187, patientId: "Z2296538" },
                     { ID: "09-11-026001", PatientName: "ADITYA DILIP", Gender: "Male", Age: "29", mobileNumber: '', patientId: "Z2277913" }
    ];

$scope.filterData = $scope.rowData;
    $scope.searchName = function() {
        $scope.searchData = $scope.quickregistration.SearchPatientId;
        if($scope.filterData != undefined ){
            $scope.rowData = $filter('filter')($scope.filterData, $scope.searchData);
        for(var key in $scope.rowData) {
                $scope.value = $scope.rowData[key];
            }       // here i got the values.. this value should be get access inside the directive

2 个答案:

答案 0 :(得分:0)

这应该很容易研究。您是否在其他任何地方寻找答案?

以下是指令与使用指令的html之间的数据绑定的简短示例。

= 的数据绑定和 @ 的文本绑定,指令中的CamelCase被html样式的连字符(attributeName - > attribute-name)替换

如果您确切知道使用此指令的控制器,则可以使用$ parent访问父作用域(例如 $ parent.someFunction())。

在HTML

<my-input-field model="modelValue" attribute-name="{{attributeName}}"></my-input-field>

在指令中:

scope: {
    model: '=model'
    attributeName: '@attributeName'
}

阅读本文:http://onehungrymind.com/angularjs-sticky-notes-pt-2-isolated-scope/

答案 1 :(得分:0)

在指令中使用@本地范围属性或=本地范围属性(用于双向绑定)

例如:

return{

scope:{
   gridheader :'=',
   rowdata :'='
  }
};

然后将这些值作为指令

中的属性传递
<my-directive gridheader="gridHeader" rowdata="rowData"></my-directive>

以下是 custom directives

的有用示例