问题
I am working on my first angularjs directive. I was hoping to wrap jquery-steps (https://github.com/rstaib/jquery-steps) into a directive. My issue comes when I try to bind inputs or expression inside the content of the steps to controller models they don't bind. An example code of what I have is below.
angular.module('foobar',[])
.controller 'UserCtrl', ($scope) ->
$scope.user =
name:'John Doe'
.directive 'wizardForm', () ->
return {
restrict: 'A',
link: (scope, ele) ->
ele.steps({})
}
The html looks as follows
<div ng-controller="UserCtrl">
<div class='vertical' wizard-form>
<h1> Step 1 </h1>
<div>Name: {{user.name}}</div>
<h1> Step 2 </h1>
<div> Advanced Info etc</div>
</div>
</div>
The output for the content in step 1 is Name: {{user.name}}
I am still a beginner with angular so I cannot seem to understand why there is no scope or model attached to the content area. Any tips or leads to get me on the right track would be very helpful!
EDIT: I added a plnkr to show what I have tried. http://plnkr.co/edit/y60yZI0oBjW99bBgS7Xd
回答1:
The following resolved this issue in my project:
.directive('uiWizardForm', ['$compile', ($compile) ->
return {
link: (scope, ele) ->
ele.wrapInner('<div class="steps-wrapper">')
steps = ele.children('.steps-wrapper').steps()
$compile(steps)(scope)
}
])
回答2:
kudos to Hugo Mallet and Nigel Sheridan-Smith. However, here was a simpler method if you want to include event handling.
.directive("uiWizardForm", function()
{
var scope;
return {
restrict: "A",
controller:function($scope){
scope = $scope;
},
compile: function($element){
$element.wrapInner('<div class="steps-wrapper">')
var steps = $element.children('.steps-wrapper').steps({
onStepChanging: function (event, currentIndex, newIndex)
{
return scope.onStepChanging();
},
onFinishing: function (event, currentIndex)
{
return scope.onFinishing();
},
onFinished: function (event, currentIndex)
{
scope.finishedWizard();
}
});
}
};
});
PS. you don't need to use the wrapInner if you already have the added in your template.
回答3:
Based on Hugo's answer, if you also want to use the jQuery steps event handling, you should do it this way (with Coffeescript):
.directive('uiWizardForm', ['$compile', ($compile) ->
return {
scope: {
stepChanging: '&',
stepChanged: '&',
finished: '&'
},
compile: (tElement, tAttrs, transclude) ->
tElement.wrapInner('<div class="steps-wrapper">')
steps = tElement.children('.steps-wrapper').steps({})
return {
pre: (scope, ele, attrs) ->
post: (scope, ele, attrs) ->
# Post-link function
ele.children('.steps-wrapper').on 'stepChanged', () ->
scope.$apply ->
return scope.stepChanging() if tAttrs.stepChanging?
true
ele.children('.steps-wrapper').on 'finished', () ->
scope.$apply ->
scope.finished() if tAttrs.finished?
ele.children('.steps-wrapper').on 'stepChanging', () ->
scope.$apply ->
scope.stepChanging() if tAttrs.stepChanging?
true
}
}
])
Then you can attach your event handlers to the scope on the directive... e.g.:
<ui-wizard-form step-changing='stepChanging()'>
maps to the $scope.stepChanging(...) ->
function.
回答4:
link: function(scope, elem, attrs){
elem.wrapInner(_handler.generateTemplate());
var _steps = elem.children('.vertical').steps({
headerTag: 'h1',
bodyTag: 'div'
});
$compile(_steps)(scope);
}
Here's on Plnkr complete solution.
回答5:
AngularJS's official site provides a step-by-step tutorial on creating custom directive. I trust if you follow through the tutorial, your question will be resolved. Particularly, please pay attention to "Isolated Scope" and "template". I guess these two issues are very relevant to your question. Good luck!
来源:https://stackoverflow.com/questions/25129845/jquery-steps-into-a-angularjs-directive