简体   繁体   English

AngularJS通过指令显示json

[英]AngularJS display json by directive

I have little problem with AngularJS, I wanted to make directive to display json imported from other website, directive working, but it doesnt show anything from angular binds(when i was using this code by just putting it hardway, everything was working). 我对AngularJS没什么问题,我想让指令显示从其他网站导入的json,指令正常工作,但它不显示角度绑定中的任何内容(当我通过艰苦使用而使用此代码时,一切正常。)

HTML HTML

<div ng-app="docsIsolateScopeDirective">
                    <div ng-controller="Controller">
                        <my-post info="post"></my-post>
                    </div>
                </div>

ANGULAR 角度

(function(angular) {


'use strict';
angular.module('docsIsolateScopeDirective', ['ngSanitize', 'ui.bootstrap'])
  .controller('Controller', ['$scope','$http','$sce', function($scope, $http, $sce) {
    $http.get(link+'json=get_recent_posts&callback=&count=1').then(function(response, date, contents){
    $scope.contents = response.data.posts;
    $sce.trustAsHtml(contents);
});
  }])
  .directive('myPost', function() {
    return {
      restrict: 'E',
      scope: {
        customerInfo: '=info'
      },
      templateUrl: 'single-post.html'
    };
  });
})(window.angular);

SINGLE-POST.HTML 单邮政.HTML

<article>
<div class="news-container">
    <span class="news-category"><a href="">{{content.categories[0].title}}</a></span>
    <h1 class="news-title">{{content.title}}</h1>
    <span class="news-date">{{content.date}}</span>
    <div class="news-image">
        <img src="{{content.thumbnail_images.full.url}}" />
    </div>
    <!-- .news-image -->
    <div class="news-entry">
        <p ng-bind-html="content.content">{{content.content}}</p>
    </div>
</div>

Any ideas? 有任何想法吗? :) :)

You save your response as 您将回复另存为

$scope.contents = response.data.posts;

And you pass into directive variable post . 然后您将指令变量传递给post Maybe you should pass contents ? 也许您应该传递contents

And also in your directive you have customerInfo not content . 而且在您的指令中,您有customerInfo content

You need to use customerInfo in the template single-post.html and also pass contents scope object to your directive 您需要在模板single-post.html使用customerInfo ,还将contents范围对象传递给指令

Additionally, Using trustAsHtml() method correctly. 此外,正确使用trustAsHtml()方法。

$scope.contents = $sce.trustAsHtml(response.data.posts);

Here is a simplified example: 这是一个简化的示例:

 (function(angular) { 'use strict'; angular.module('docsIsolateScopeDirective', []) .controller('Controller', ['$scope', function($scope) { $scope.contents = { title: "test" }; } ]) .directive('myPost', function() { return { restrict: 'E', scope: { customerInfo: '=info' }, template: '<article>\\ <h1 class="news-title">{{customerInfo.title}}</h1>\\ </article>' }; }); })(window.angular); 
 <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="docsIsolateScopeDirective"> <div ng-controller="Controller"> <my-post info="contents"></my-post> </div> </div> 

its always better to use jsonp for calls to outer domains. 使用jsonp调用外部域总是更好。 Can you try doing this? 你可以尝试这样做吗?

$http.jsonp('some/trusted/url', {jsonpCallbackParam: 'callback'}) $ http.jsonp('some / trusted / url',{jsonpCallbackParam:'callback'})

Whitelisting urls is also apt. 将网址列入白名单也很合适。

Pass $scope.contents to info. 将$ scope.contents传递给info。

 $scope.contents = response.data.posts;

<div ng-app="docsIsolateScopeDirective">
                    <div ng-controller="Controller">
                        <my-post info="contents"></my-post>
                    </div>
                </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM