angular的雙向資料繫結

2022-09-01 16:36:10 字數 3600 閱讀 8847

方向1:模型資料(model) 繫結 到檢視(view)

實現方法1:}        

$scope.num=10

}

實現方法2: 常用指令(ngrepeat、ngif、ngshow/hide/src....) 

$scope.list=[,,]

}

}

方向2:將檢視(view)中使用者輸入的資料 繫結到 模型資料(model)

實現方法:ngmodel指令 用在表單元件中(input select textarea。。。)

ps:$watch監聽模型變數值的改變------>執行指定的方法

$scope.$watch('變數名',function());

1

doctype html

>

2<

html

>

3<

head

lang

="en"

>

4<

meta

charset

="utf-8"

>

5<

title

>

title

>

6<

script

src="js/angular.js"

>

script

>

7head

>

8<

body

ng-controller

="myctro"

>910

11 核取方塊:<

input

type

="checkbox"

ng-model

="isagree"

/>

12<

br>

1314

1516

<

select

ng-model

="sel2"

ng-options

="sel2.name for sel2 in selection"

>

select

>

17<

p>}

p>

18<

script

>

19var

=angular.module(''

,['ng'

]);2021'

myctro',

function

($scope));

26$scope.$watch(

'sel',

function

());

2930

$scope.selection=[

31,32,

33,3435

];36

//為select標籤設定乙個selected 選項

37$scope.sel2

=$scope.selection[1];

3839

$scope.$watch(

'sel2',

function

() )

42})

43script

>

44body

>

45html

>

3、利用ng-model  ng-checked  雙向資料傳輸 實現全選/部分選擇的判斷

doctype html

>

<

html

>

<

head

lang

="en"

>

<

meta

charset

="utf-8"

>

<

title

>

title

>

<

script

src="js/angular.js"

>

script

>

head

>

<

body

ng-controller

="myctro"

>

<

table

>

<

thead

>

<

tr>

<

th>請選擇

th>

<

th>姓名

th>

<

th>生日

th>

tr>

thead

>

<

tbody

>

<

tr ng-repeat

="tem in list"

>

<

td>

<

input

type

="checkbox"

ng-model

="tem.ischecked"

ng-checked

="selectall"

/>

td>

<

td>}

td>

<

td>}

td>

tr>

tbody

>

table

>

<

input

type

="checkbox"

ng-model

="selectall"

/>

全選/取消全選

<

br/>

<

button

ng-click

="getmsg()"

>檢視

button

>

<

script

>

var=

angular.module(''

,['ng'

]);'

myctro',

function

($scope),

];//監聽的目的:通過全選或者取消全選時,ng-checked方向1的繫結確實會生效

//當時不會直接修改

angular元件雙向繫結

在寫專案時,需要編寫乙個元件,根據使用者選擇的單選框返回值,就像元件的雙向繫結。元件的雙向繫結就是子元件接受父元件的資料,父元件監聽子元件的事件來修改自己的值.output statechange instrumentstatechange new eventemitter 暴露乙個statecha...

Angular4的雙向資料繫結

最近在學angular4,因為angularjs中預設是雙向資料繫結,但是如果在比較複雜的頁面使用的話會引起效能問題,那是因為angularjs會在頁面儲存乙個所有資料繫結的列表,每當資料發生變化時,angularjs都會反覆檢視列表,以保證資料一致性。然後在angular4中預設是單向資料繫結。但...

Angular 實現元件間雙向資料繫結

學過angular的同學都知道,輸入框通過 ngmodel 實現雙向資料繫結,那麼父子元件間能不能實現雙向資料繫結呢?答案是肯定的。angular中,我們常常需要通過方括號和圓括號 實現元件間的互動 那麼在和 的基礎上,如何實現元件的雙向資料繫結?例子如下。子元件 toggle childstatu...