Original intention
The previous article has implemented two-way binding of the data, but the model control is the entire document, in the actual project must have a scope, in order to do the UI module split.
In this article, we want to implement controller inheritance like Angularjs:
1. The model of the parent controller can be accessed in the child controller
2. The model of the child controller does not affect the parent controller
3. Controller inheritance is specified in HTML, not in JS
Goal
In HTML, use the Isi-controller property to declare the controller:
<body> <div isi-controller="Parentcontroller"> <input data-bind="name"> <div isi-controller="Subcontroller"> <input data-bind="name"> </div> </div></Body
Hope that the above input name changed, the following will change, and the following changes, the above unchanged.
JS, define the controller with a function with the same name as above Isi-controller property value:
function ParentController() { varnew Model(); model.set(‘name‘‘parent‘);}function ParentController() { varnew Model(); model.set(‘name‘‘sub‘);}
For the user, just write this and it's over.
Realize
Very simple JS bidirectional binding framework (II): Controller inheritance