2016-05-19 15 views
1

ここでは、angularJsディレクティブとコントローラを使用してサンプルプログラムを作成しました。imはいくつかの属性を作成しました(print-Display = "true" is-Silent = "false"することができます、ボタン(コントローラ)事前にサブミットボタンを使用してディレクティブの属性値を取得する方法

おかげ

scope.$parentことで

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

 
app.directive('telPrint', ['$http', 
 
    function($http) { 
 
    return { 
 
     restrict: 'AEC', 
 
     require: 'ngModel', 
 
     scope: { 
 
     ngModel: '=', 
 
     printDisplay: '@', 
 
     canShow: '@', 
 
     isChecked: '@', 
 
     isAuthentication: '@', 
 
     isDownload: '@', 
 

 
     }, 
 
     template: '<span > <input type="checkbox" name="ngModel" ng-checked="checked"> <label > Print </label></span> ', 
 
     controller: function($scope, $rootScope) { 
 
     $scope.printOption = []; 
 
     $scope.Authentication = $scope.isAuthentication; 
 
     $scope.Silent = $scope.isSilent; 
 
     $scope.Download = $scope.isDownload; 
 

 
     }, 
 

 
     link: function(scope, iElement, iAttrs, ngModelController) { 
 
     var labelName = iAttrs.getString; 
 
     var labelName = labelName.split(","); 
 
     var language = labelName[0]; // Language EN or Fr 
 
     var fieldlabelName = labelName[1]; // Label Name 
 
     var fieldmoduleName = labelName[2]; // Module Name (global or local) 
 

 

 

 

 
     if (iAttrs.isSilent == "true") { 
 
      scope.checked = true; 
 
     } else if (iAttrs.isSilent == "false") { 
 
      scope.checked = false; 
 
     } 
 

 
     console.log(iAttrs.printDisplay) 
 

 

 

 

 
     } 
 
    }; 
 
    } 
 
]) 
 

 

 
app.controller('myCtrl', function($scope) { 
 

 
    $scope.printcontr = function() { 
 
    console.log("Inside ") 
 

 
    } 
 

 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
 

 
<body ng-app="myApp" ng-controller="myCtrl"> 
 
    <div class="row col-sm-12"> 
 
    <div class="col-sm-12"> 
 
     <tel-Print ng-model="taxmaster.print" get-string="lbl,print,common" print-Display="true" is-Silent="true" is-Authentication="false" is-Download="true"></tel-Print> 
 
    </div> 
 
    <div> 
 
     <div class="col-sm-12"> 
 
     </div> 
 

 
     <div class="col-sm-12"> 
 
     <input type="submit" value="Save" ng-click="printcontr()" /> 
 
     </div> 
 

 
    </div> 
 
</body>

答えて

0

を提出使用している間、この属性の値を取得する方法を、 "偽")= "false" を、ダウンロードでアクセスSコントローラの$scope

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

 
app.directive('telPrint', ['$http', 
 
    function($http) { 
 
    return { 
 
     restrict: 'AEC', 
 
     require: 'ngModel', 
 
     scope: { 
 
     ngModel: '=', 
 
     printDisplay: '@', 
 
     canShow: '@', 
 
     isChecked: '@', 
 
     isAuthentication: '@', 
 
     isDownload: '@', 
 

 
     }, 
 
     template: '<span > <input type="checkbox" name="ngModel" ng-checked="checked"> <label > Print </label></span> ', 
 
     controller: function($scope, $rootScope) { 
 
     $scope.printOption = []; 
 
     $scope.Authentication = $scope.isAuthentication; 
 
     $scope.Silent = $scope.isSilent; 
 
     $scope.Download = $scope.isDownload; 
 

 
     }, 
 

 
     link: function(scope, iElement, iAttrs, ngModelController) { 
 
     var labelName = iAttrs.getString; 
 
     var labelName = labelName.split(","); 
 
     var language = labelName[0]; // Language EN or Fr 
 
     var fieldlabelName = labelName[1]; // Label Name 
 
     var fieldmoduleName = labelName[2]; // Module Name (global or local) 
 

 

 

 

 
     if (iAttrs.isSilent == "true") { 
 
      scope.checked = true; 
 
     } else if (iAttrs.isSilent == "false") { 
 
      scope.checked = false; 
 
     } 
 

 
     console.log(iAttrs.printDisplay); 
 

 

 
     // Set data 
 
     scope.$parent.language = labelName[0]; 
 
     scope.$parent.fieldlabelName = labelName[1]; 
 
     scope.$parent.fieldmoduleName = labelName[2]; 
 

 
     } 
 
    }; 
 
    } 
 
]) 
 

 

 
app.controller('myCtrl', function($scope) { 
 

 
    $scope.printcontr = function() { 
 
    console.log("Inside "); 
 

 
    //Retrive data 
 
    console.info('language=', $scope.language); 
 
    console.info('fieldlabelName=', $scope.fieldlabelName); 
 
    console.info('fieldmoduleName=', $scope.fieldmoduleName); 
 

 
    } 
 

 

 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script> 
 

 
<body ng-app="myApp" ng-controller="myCtrl"> 
 
    <div class="row col-sm-12"> 
 
    <div class="col-sm-12"> 
 
     <tel-Print ng-model="taxmaster.print" get-string="lbl,print,common" print-Display="true" is-Silent="true" is-Authentication="false" is-Download="true"></tel-Print> 
 
    </div> 
 
    <div> 
 
     <div class="col-sm-12"> 
 
     </div> 
 

 
     <div class="col-sm-12"> 
 
     <input type="submit" value="Save" ng-click="printcontr()" /> 
 
     </div> 
 

 
    </div> 
 
    </div> 
 
</body>

0

私はあなたがディレクティブをフェッチする必要がある場合ディレクティブのスコープを定義する代わりに使用している間、親コントローラ内の属性を示唆している「@」、使用「=」ディレクティブ内の属性を変更しても、親コントローラには常にその値が設定されます(双方向バインディング)。

app.directive('telPrint', ['$http', 
    function($http) { 
    return { 
     restrict: 'AEC', 
     require: 'ngModel', 
     scope: { 
     ngModel: '=', 
     printDisplay: '=', 
     canShow: '=', 
     isChecked: '=', 
     isAuthentication: '=', 
     isDownload: '=', 

     }... 
関連する問題