1

我能够从简单的 html 和 javascript 中获取并显示联系人照片,但是当我使用 angularjs 模型显示联系人照片时,出现错误。以下是我的源代码:

列出我试图显示联系人的位置:

<ul class="list">

   <li class="item item-thumbnail-left" ng-repeat="contact in contactList">
            <img ng-src="{{contact.mphoto}}"/> <!--When I put this path directly ( ie "content://com.android.contacts/contacts/30/photo"), I am able to display the image, but when I fetch it from the controller, I am getting error: "unable to read contacts from asset folder" -->
           <h2>{{contact.name}}</h2>
           <p >{{contact.number}}</p>

   </li>
</ul>

这是我用于设置 ContactList 的控制器:

ContactService.find("").then(function(contacts) {
        for (var i = 0; i < contacts.length; i++)
        {
            if (contacts[i].phoneNumbers !== null)
            {
                for (var j = 0; j < contacts[i].phoneNumbers.length; j++)
                {
 var img = contacts[i].photos  != null ? contacts[i].photos[0].value : "img/default.png";

                    $scope.contactList.push({name: contacts[i].name.formatted, number: contacts[i].phoneNumbers[j].value, mphoto: img})
                }
            }
        }
4

2 回答 2

10

经过这么多的努力,我终于能够找到问题所在,

请将以下行粘贴到您的 App.js 文件中,问题将得到解决,不显示照片的原因是 Angularjs 在每个 url 之前添加了 unsafe: 如果它不受信任。

 config(['$compileProvider', function($compileProvider) {
            $compileProvider.imgSrcSanitizationWhitelist(/^\s*(https?|ftp|file|blob|content):|data:image\//);
        }]);
于 2014-07-14T10:16:46.257 回答
0

我在带有 Ionic 2 的 Angular 2 中遇到了这个问题,但我不知道这是问题所在,我不知道如何在 Angular 2 中尝试接受者的答案。为了完整起见,这里是你将如何解决它使用离子 2:

注入sanitizer: DomSanitizer您的控制器/服务。

然后调用:sanitizer.bypassSecurityTrustUrl(photoURL)

这是一个例子:

export class HomePage {
  url;

  constructor(public navCtrl: NavController, platform: Platform, sanitizer: DomSanitizer) {
    platform.ready().then(() => {
      Contacts
        .pickContact()
        .then((contact) => {
          alert(JSON.stringify(contact));
          if (contact.photos) {
            var photoURL = contact.photos[0].value;
            this.url = sanitizer.bypassSecurityTrustUrl(photoURL);
          }
        });

    })
  }

}
于 2016-11-12T20:35:51.047 回答