Reputation: 6197
here is the jsfiddle. HTML:
<div ng-app="app">
<div ng-controller="ctrl">
<div my-rd name="gender" value="male" model="gender"></div>
<div my-rd name="gender" value="female" model="gender"></div>
<p>Your choice is: {{gender}}</p>
</div>
</div>
JS:
angular.module("app", [])
.directive("myRd", function(){
return{
restrict: "A",
scope: {
name: "@",
value: "@",
model: "="
},
template: "<input name='{{name}}' ng-model='model' value='{{value}}' checked type='radio' />{{value}}"
}
})
.controller("ctrl", function($scope){
})
I created a directive which can generate a custom radio button with custom attributes. The problem is I can't set ng-model name correctly and the "checked" property doesn't work as well.
Please give me a hand, many thanks!
Upvotes: 0
Views: 1290
Reputation: 6060
You use the shorthand syntax =
that means the attribute name is the same as the value you want to bind to inside the directive's scope.
If the declaration is <div my-rd foo="test">
then you have to specify in your directive
model: "=foo" //It tells $compile to bind to the foo attribute.
Here in your directive you can access the value
//directive will know only the property inside scope:{'your_propery': value}
//to access the value inside directive {{your_propery}}
scope.model //{{model}}
And in your controller you can access the value
$scope.test //{{test }}
Upvotes: 1