SoFunction
Updated on 2025-04-11

Detailed explanation of AngularJS bidirectional binding and dependency inversion instance

AngularJS Bidirectional Binding and Dependency Inversion

1. Two-way binding:

UI<-->Data

Data -> UI (Data changes UI and change)

UI->Data (UI changes data and changes)

Data Change->UI Change Principle:

Listen to whether the data has changed, and update the UI data if it has changed.

UI Change->Data Change Principle:

&lt;html&gt;

&lt;body&gt;

&lt;input type="text" name="name" value=""  ng_model="a"&gt;

&lt;script&gt;

 = function(){

var a='';

var oTxt = ('text1');

 = function(){ // UI value changes data changes
a = ;

}

}

&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

 

2. Dependency injection:

Functions can decide what data or how many small data you need, instead of using whatever is transmitted outside.

2.1. The caller decides to give more small parameters

&lt;script&gt;

function show(a,b,c){

();

}

show(1); //The caller only gives 1 parameter, and the caller decides to give the parameter.
&lt;/script&gt;

2.2. Dependency injection (dependency inversion): The smaller parameters are required for the function. Just like show(a,b,c) requires 3 parameters

&lt;script&gt;

function show(a,b,c){

();

}

var scope = {a:12,b:15,c:99,qq:55,i:99};  //Suppose it is the parameter required by the function
//Two steps to implement dependency inversion//1. Know what parameters to show
var str = ();

str=('{')[0].match(/\(.*)\/)[0].replace(/\S+/g,'');

str=(1,-1);

var arr=(',');

//2. Give it the corresponding value
var args=[];

for(var i=0;i&lt;;i++){

args[i]=scope[arr[i]];

}

(args);

(null,args);

&lt;/script&gt;

Thank you for reading, I hope it can help you. Thank you for your support for this site!