# 新闻API示例

在这里您将找到一些关于如何使用 [`news_provider` 小部件构造函数选项](https://zlq4863947.gitbook.io/tradingview/4-tu-biao-ding-zhi/widget-constructor#news_provider) 的示例。

## 设置小部件标题

要设置新闻小部件标题，请使用可选的 `title` 属性。

```javascript
new TradingView.widget({
    /* 为简单起见隐藏其他小部件选项 */
    news_provider: function getNews(symbol, callback) {
        callback({
            title: 'This is the title!',
            newsItems: [/* ... */]
        })
    }
});
```

## 获取非 RSS 新闻

假设我们有一个 API 端点，它返回与 `NewsItem` 接口匹配的新闻项的 JSON 表示。

```javascript
const jsonNewsApiUrl = 'https://www.example.com';

new TradingView.widget({
    /* 为简单起见隐藏其他小部件选项 */,
    news_provider: function getNews(symbol, callback) {
        fetch(jsonNewsApiUrl)
            .then(res => res.json())
            .then(json => {
                callback({
                    newsItems: json,
                });
            });
    }
});
```

## 按需更新新闻

假设我们想按需刷新新闻，例如在一些用户事件之后。 我们可以使用 `INewsApi` 的`update` 方法。

```javascript
const widget = new TradingView.widget({
    /* widget options hidden for simplicity */
});

function someEventHandler() {
    widget.news().then(newsApi => newsApi.refresh());
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://zlq4863947.gitbook.io/tradingview/fu-lu/news-api-examples.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
