In practice, there are often data with consistent data structures but in-depth uncertainties. In general, data processing uses recursive methods. For example, the presentation of an unlimited category of a website and the structure of the classified data:
Var cate = [
{
CateId: 1,
CateName: 'frontend techno ',
Child :[
{
CateId: 4,
CateName: 'javascript'
},
...
]
},
{
Cate: 2,
CateName: 'Back-end technical ',
Child :[
{
CateId: 3,
CateName: 'php ',
Child :[
{
CateId: 6,
CateName: 'thinkphp'
},
...
]
}
]
}
];
Jq is often implemented in the following way. The sample code is as follows:
/**
** Parse the template file
* @ Param template string
* @ Param scope Template scope
* Return [string] the parsed string
*/
Function templateParse (template, scope ){
If (typeof template! = "String") return;
Return template. replace (/\ {\ w + \}/gi, function (matchs ){
Var returns = scope [matchs. replace (/(\ {) | (\})/g, "")];
Return (returns + "") = "undefined "? "": Returns;
});
}
/**
* Parse and insert a category
*/
$ ('# Category'). append (function (){
Var template = '<a href = "{cateid).html"> {cateName} </a> ';
Var ret = (function (c ){
If (! C |! C. length) return '';
Var ret = '<ul> ';
For (var I = 0, j = c. length; I <j; I ++ ){
Ret + = '<li> ';
Ret + = templateParse (template, c [I]);
Ret + = arguments. callee (c [I]. child );
Ret + = '</li> ';
}
Return (ret + '</ul> ');
}) (Cate );
Return ret;
});
On the above page, you can click "jq achieves unlimited classification demonstration DEMO" to view the corresponding results.
Implementation of template-based recursion in angularJs
In the same principle, you can combine the built-in ng-include and ng-init of angularJs to achieve recursion. The example template is as follows:
<Script id = "recursion" type = "text/ng-template">
<Li ng-repeat = "item in cate">
<A href = "{item. cateId}" >{{ item. cateName }}</a>
<Ul ng-if = "item. child. length" ng-include = "'recursion'" ng-init = "cate = item. child"> </ul>
</Li>
</Script>
The call method is as follows:
<Div ng-app = "demo">
<Div ng-controller = "demo">
<Ul ng-include = "'recursion'"> </ul>
</Div>
</Div>
DEMO of implementation effect demonstration, "AngularJ achieves classification display based on template recursion"
Implementation based on command recursion
In the same way, in commands, we can do this (for details, refer to the self-angular-recursion ):
Angular. module ('demo'). directive ('recursion', function ($ compile ){
Function cpl (element, link ){
// Normalize the link parameter
If (angular. isFunction (link )){
Link = {post: link };
}
// Break the recursion loop by removing the contents
Var contents = element. contents (). remove ();
Var compiledContents;
Return {
Pre: (link & link. pre )? Link. pre: null,
/**
* Compiles and re-adds the contents
*/
Post: function (scope, element ){
// Compile the contents
If (! CompiledContents ){
CompiledContents = $ compile (contents );
}
// Re-add the compiled contents to the element
CompiledContents (scope, function (clone ){
Element. append (clone );
});
// Call the post-linking function, if any
If (link & link. post ){
Link. post. apply (null, arguments );
}
}
};
}
Return {
Restrict: 'A ',
Scope: {recursion: '= '},
Template: '<li ng-repeat = "item in recursion"> \
<A href = "too many item.cateid=}.html" >{{ item. cateName }}</a> \
<Ul recursion = "item. child"> \
</Ul> \
</Li> ',
Compile: function (element ){
Return cpl (element, function (scope, iElement, iAttrs, controller, transcludeFn ){
// Define your normal link function here.
// Alternative: instead of passing a function,
// You can also pass an object
// A 'Pre'-and 'post'-link function.
});
}
};
});
Recursion commands
The above two methods are too tightly coupled with the template. Is there a way to use them like the following? (Not finished, continued)
<Ul recursion = "cate">
<Li ng-repeat = "item in cate">
<A href = "{item. cateId}" >{{ item. cateName }}</a>
<Ul recursion-child = 'item. Kids'> </ul>
</Li>
</Ul>