繁体   English   中英

如何在angularJs中将值从控制器访问到指令

[英]How to access the value from the controller to Directive in angularJs

我在控制器中得到了值。 我希望这些值能在指令中得到访问。 可能吗..?

$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

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

这是指令与您使用指令的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/

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

例如:

return{

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

然后将这些值作为属性传递给指令

<my-directive gridheader="gridHeader" rowdata="rowData"></my-directive>

这是有关自定义指令的有用示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM