简体   繁体   中英

Angularjs passing ng-click to directive

i'm fairly new to angularjs and i seem to have got myself in a twist of code and cant seem to figure this out.

here is the code (angularjs)

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


// ---- Controllers ---- 

siteOptimizeApp.controller('Formctrl',['$scope', '$http', function($scope, $http){

$scope.customer = customer;

$scope.placeholderWrp = '';
$scope.inputWrp = 'hidden';
$scope.modal = 'modal';
$scope.customer.cancelfunc = 'customer.cancel()';

$scope.customer.edit = function(){
    $scope.this.placeholderWrp = 'hidden';
    $scope.this.inputWrp = '';
}

$scope.customer.cancel = function(){
    $scope.this.placeholderWrp = '';
    $scope.this.inputWrp = 'hidden';
}

$scope.customer.modal = function(){

}

$scope.formSubmit = function(){

}

}]);


// ---- Directives (element) ---

// remove-btn
siteOptimizeApp.directive('removeBtn', function(){
return {
    restrict: 'E',
    //replace: true,
    scope: {
        customerInfo: '=info'
    },
    template: '<button type="button" class="close" data-dismiss="{{customerInfo}}" ><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>'
}
});

where customer is -

customer = {"birthday":"0000-00-00","customersettings":null,"email":"j_email@gmail.com","fullname":"j_email","gender":"m","cid":"cust_D5C502813C62017","location":"localhost","logintype":"email","timeszone":null}

and here is the html :

<form name="profileFrm" action="" novalidate="" ng-controller="Formctrl" ng-submit="formSubmit()">
<div class="modal-header">
    <remove-btn info="modal"></remove-btn>
    <h4 class="modal-title">Edit Profile</h4>
</div>
<div class="modal-body">
   <p class="row">
        <label for="fullname" class="col-md-3 col-lg-3 col-sm-12 control-label">Full Name:</label>
        <span class="col-md-6 col-lg-6 col-sm-12 form-control-static {{placeholderWrp}}">
              {{customer.fullname}}
              <a class="edit" href="#" ng-click="customer.edit()" >Edit
                  <i class="glyphicon glyphicon-edit"></i>
              </a>
        </span>
        <span class="col-md-6 col-lg-6 col-sm-12 form-control-static {{inputWrp}}">
              <input ng-model="customer.fullname" class="" id="fullname" type="text" name="fullname" placeholder="Enter Your full name" value="{$fullname}"/>
              <remove-btn info="customer.cancelfunc"></remove-btn>
        </span>
   </p>
</div>
</form>

As you can see <remove-btn info=""></remove-btn> is the custom directive that i have defined. When i pass in the 'info' attribute as 'modal' (.ie 'info="modal"') it works fine and translates into the following html - <remove-btn info="modal" class="ng-isolate-scope"> <button type="button" class="close" data-dismiss="modal"> <span aria-hidden="true">×</span> <span class="sr-only">Close</span> </button> </remove-btn>

which is basically a bootstrap handled button for closing the modal.

Here is where i'am stuck: Now how do i pass an ng-click to this custom directive for cases other then when the attr is 'info="modal"' (.ie 'info="somethingelse") coz i want to be able to use this across the site and probably cancel or close different elements at different instances.

Looks like the answer was simple.

i can use ng-click directive as attribute on my custom directive as so -

<remove-btn info="input" ng-click="customer.cancel()"></remove-btn>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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