2015-04-05 30 views
0

我知道標題很糟糕,但情況本身足夠奇怪,無法使用該標題。因此,這裏是我的玉文件 - 模板:指令範圍變量不能在Jade中工作

img.picture(ng-show='user.info.profilePicUrl', ng-src='{{user.info.profilePictureUrl}}') 
img.picture(ng-show="!user.info.profilePicUrl", ng-src="dummyuser.png") 
.basicinfo 
    h1.fullname 
    a(href='profile/{{user.info.username}}') {{user.info.fullName}} 
    p.description {{user.info.bio}} 
    span.additional 
    a.website(href='{{user.info.website}}') {{user.info.website}} 
    a.location(href='#') {{user.info.location}} 
.follow 
    .count {{user.followedBy.length}} Followers 
    follow(to-follow-username="user.info.username") 

在指令:

scope: { 
    user: "=" 
}, 
controller: function($scope){                      
    var request = $http({                        
     method: "get",                         
     url: "https://stackoverflow.com/users/" + $scope.user.userID + "/getInfo",                
    });                            

    request.success(function(data, status, headers, config){               
     $scope.user.info = data;                      
     console.log($scope.user);                      
    });                            

    request.error(function(data, status, headers, config){               
     console.log("Status");                       
     console.log(status);                       
    });                            
    },  
templateUrl: "/templates/follower" 

因此,這裏的問題:當我打開包含這個模板和指令的頁面,用戶被傳遞到翡翠以上,並且它的每一行都分析user的數據,但profilePicUrluser.info.username除外。 user.info.username在行a(href='profile/{{user.info.username}}') {{user.info.fullName}}中正常工作,但它不起作用 - 以undefined的形式出現 - 在行follow(to-follow-username="user.info.username")中。

img.picture(ng-show='user.info.profilePicUrl', ng-src='{{user.info.profilePictureUrl}}') 
img.picture(ng-show="!user.info.profilePicUrl", ng-src="dummyuser.png") 
.basicinfo 
    h1.fullname 
    a(href='profile/{{user.info.username}}') {{user.info.fullName}} //The username is actual username - Works Here 
    p.description {{user.info.bio}} 
    span.additional 
    a.website(href='{{user.info.website}}') {{user.info.website}} 
    a.location(href='#') {{user.info.location}} 
.follow 
    .count {{user.followedBy.length}} Followers 
    follow(to-follow-username="user.info.username") //Doesn't work here 
+0

什麼'p {{用戶}}'顯示? – KyleK 2015-04-05 21:30:42

+0

是的,它只打印沒有'user.info'的'user'。我根據這個編輯了這個問題。添加了我的指令的控制器功能。我怎樣才能使http調用同步? – mtndesign 2015-04-07 10:33:20

+0

我用諾言(然後())來完成這個,但錯誤仍然存​​在。即使數據來源於Jade,Jade表達式也會解析它,而'ng- *'不會:'ng-show =「!user.info.profilePicUrl」'不起作用。 – mtndesign 2015-04-08 10:13:31

回答

1

不要讓HTTP CAL同步,但重新申請成功的範圍修改:

request.success(function(data, status, headers, config){ 
    $scope.$apply(function() { 
    $scope.user.info = data; 
    }); 
}); 
+0

不幸的是沒有運氣。由於異步HTTP調用,模板馬上呈現 - (我對此的愚蠢觀點) - 是否有任何方法讓模板在控制器返回或「請求」結束後呈現? – mtndesign 2015-04-09 09:35:41

+0

$應用重新渲染模板。這不應該是問題。檢查data.username包含一個字符串,然後你有任何控制檯中的錯誤? – KyleK 2015-04-09 10:42:47