iT邦幫忙

0

動態載入html並使用angularjs bind data

各位好
目的:按下按鈕後, 把檔案中的html讀出放到$scope.html裡, 再data bind改值
以下是目前的code, 但要按兩下才會出現

$http.get(url)    
.then(function(rsp) {
    var InnerHtml = "";
    jQuery.get('MyHTML.ejs', function(data) {
        console.log(data);
        $scope.html = data;
    });
    $scope.test = "Hi there";

}, function myError(rsp) {
    //failed
});

直接給字串是可以成功的:

$http.get(url)    
.then(function(rsp) {
    var InnerHtml = '<p ng-bind="test">aaa</p>';
    $scope.html = InnerHtml;
    $scope.test = "Hi there";

}, function myError(rsp) {
    //failed
});

請問是哪裡有問題?

看更多先前的討論...收起先前的討論...
阿薩姆 iT邦新手 4 級 ‧ 2019-11-04 16:26:39 檢舉
會是非同步的問題嗎
giulian iT邦新手 4 級 ‧ 2019-11-04 17:39:16 檢舉
是,你的scope.html應該是作為template用,可以調用一次就好,你目前這樣做會變成每次按都調用一次然後再塞資料進去
阿薩姆 iT邦新手 4 級 ‧ 2019-11-05 09:43:30 檢舉
那請問通常都怎麼做?
giulian iT邦新手 4 級 ‧ 2019-11-05 15:08:24 檢舉
我個人的話通常是頁面一進來先讀template,點擊後再撈資料丟給template 去render,angular我不熟,但邏輯應該不會差太遠
阿薩姆 iT邦新手 4 級 ‧ 2019-11-08 10:40:24 檢舉
giulian有考慮直接回答嗎? XD
目前也是採用您的建議~
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友回答

立即登入回答