iT邦幫忙

0

(已自行解決)MVC AngularJS 後端資料傳到前端頁面取值問題

各位大大你們好,
如題,
最近在實作MVC結合Angular的網頁,
但這兩天一直卡在前端取值的地方,
網路上查過很多相關方法都不行,
可能我還沒融會貫通…請高人指點迷津/images/emoticon/emoticon02.gif

首先,
前端頁面我已經能用ng-repeat的方式將所有我想要的資料呈現出來:
tests:是Controller去呼叫後端取得的值。
id, name, add則是資料庫中存的資料。

<tr>
    <th>編號</th>
    <th>姓名</th>
    <th>地址</th>
</tr>
<tr ng-repeat="SM01 in SM01s">
    <td>{{test.id}}</td>
    <td>{{test.name}}</td>
    <td>{{test.add}}</td>
</tr>

但是因為排版需求,所以要用下面的寫法:

<tr ng-repeat="test in tests">
    <td>編號</td>
    <td>{{test.id}}</td>
</tr>
<tr ng-repeat="test in tests">
    <td>姓名</td>
    <td>{{test.name}}</td>
</tr>
<tr ng-repeat="test in tests">
    <td>地址</td>
    <td>{{test.add}}</td>
</tr>

但我知道這寫法真得很蠢…
有種一直用for迴圈叫值的方式…

所以我想改成類似這樣直接呼叫tests存的對應資料就好,

<td class="form_1td_label_width"><label for="year">編號</label></td>
<input class="form-control" id="id" ng-model="tests.id" type="text"><br>
<td class="form_1td_label_width"><label for="year">姓名</label></td>
<input class="form-control" id="name" ng-model="tests.name" type="text"><br>
<td class="form_1td_label_width"><label for="year">地址</label></td>
<input class="form-control" id="add" ng-model="tests.add" type="text"><br>

但是改成這樣值一直抓不到…

後端Controller取值的code大概類似這樣,
Service.Test(id).then(function (response) {
$scope.tests = response.Data;
},

#有測試過 $scope.tests.id = '123'; 前端頁面是有get到123的,
但是只要改成 $scope.tests.id = response.id; 它就什麼反應都沒有了…

再麻煩有理解這塊的大大指點一下…

#編輯
剛剛發現Chrome有擴充工具能幫忙檢視前端抓的資料,
結果發現我的tests裡面根本沒撈到值…
繼續研究中…

#1081113更
有時候發問完沒登入沒回答就會忘記自己的問題…
印象中解決方式要依回傳值的型態而定,
群組的話 $scope.tests = response.Data[0]; 就會有值啦~

rewrite iT邦新手 3 級 ‧ 2019-01-15 11:55:22 檢舉
你的 response 可以先console.log看看,值沒有存進去可能是賦值格式錯誤
石頭 iT邦高手 1 級 ‧ 2019-01-15 23:44:11 檢舉
可提供 response json 資料?
yorklin iT邦新手 5 級 ‧ 2019-01-16 14:40:43 檢舉
@rewrite 值確定沒錯誤
@dog830228 這個…因為資料都是從資料庫撈出來的機密資料,所以沒辦法提供,晚點會再編輯一下有成功的結果與疑惑。
感覺是我對 $scope 的運用概念太模糊。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答