簡體   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