【问题标题】:angular2 get string in json format on clickangular2点击时获取json格式的字符串
【发布时间】:2016-08-09 13:29:43
【问题描述】:

我正在尝试使用 click 事件从 json 响应中获取标题,当我单击按钮时我设法获取所有标题,但是如何根据 button 或 @ 获取特定标题987654323@用户点击了吗?

modalGetTitle(title) {
    this.http.get('../../xmlConf/dashboard_journey.json')
    .map((res:Response) => res.json())
    .subscribe(data => {
        if(data) {
            var jsonObj = JSON.parse(JSON.stringify(data));
            this.oJourney = jsonObj.o.journey;
            this.getJourneyTitle = this.oJourney; 

            for (var i = 0; i < this.getJourneyTitle.length; i++) {
                var element = this.getJourneyTitle[i];
                console.log(element.title);
            }      
        }

    });
};

喜欢 如果我点击&lt;button (click)="modalGetTitle()"&gt;Title 1&lt;/button&gt; 并且我想在&lt;span&gt; 中呈现json 响应标题,因此该按钮只是为了触发该功能。

Atm 我只得到响应中的最后一项:

<a href="javascript:;" (click)="modalGetTitle()" class="float-shadow" *ngIf="journey.journey_url == 'javascript:;' ">{{journey.title}} </a>

【问题讨论】:

    标签: javascript jquery json angular


    【解决方案1】:

    如果我理解你的问题,你可以试试这个。

    <button ng-click="modalGetTitle()"><span id="spanId">Title</span></button>
    

    在控制器中你可以定义函数

              $scope.modalGetTitle = function(){
    var title = document.getElementById('spanId').innerHTML
                 this.http.get('../../xmlConf/dashboard_journey.json')
                    .map((res:Response) => res.json())
                    .subscribe(data => {
                        if(data) {
                            var jsonObj = JSON.parse(JSON.stringify(data));
                            this.oJourney = jsonObj.o.journey;
                            this.getJourneyTitle = this.oJourney; 
    
                            for (var i = 0; i < this.getJourneyTitle.length; i++) {
    
                                var element = this.getJourneyTitle[i];
            if(element == title){
                                console.log(element.title);
            }
                            }      
                        }
                    });
                }
    

    【讨论】:

    • 但这只会让我得到硬编码范围内的任何内容?我打算在 中显示标题。按钮只是触发功能。
    • 在这种情况下,您可以在这样的函数中设置标题 var title = document.getElementById('spanId').innerHTML;
    • 如果语句没有返回任何内容,我认为我不需要获取文档,因为我可能只是在视图中{{ title }}。请参阅我对蒂埃里的回复。
    • 我不明白你想要什么。我在jsfiddle上放了一个简单的例子,如果你不能使用,请发布你在做什么
    • 谢谢,虽然它在 angular1 中,但它给了我这个想法并且是盲目的。我的跨度超出了范围,这就是为什么我什么都得不到。
    【解决方案2】:

    你可以传递一些东西作为你的方法的参数。例如:

    <button (click)="modalGetTitle('title1')">Title 1</button>
    

    【讨论】:

    • 抱歉,您能再解释一下吗?按钮只是为了触发该功能,我想在 中显示 json 响应标题
    • 抱歉,我不确定你想做什么。您将从响应负载中选择要显示的标题是什么?
    • 是的,对不起,我听起来很困惑,我有一个链接集合(例如 JS),带有 (click) 事件来触发函数并获得带有标题的响应(JS) 并在 标签中显示响应。
    • 我的代码只返回了 json 响应中的最后一项。
    【解决方案3】:

    这样做

    <button (click)="modalGetTitle(this)">Title 1</button>
    function modalGetTitle() {
        alert(objButton.textContent); // this will show "Title 1"
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-16
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-25
      • 2014-09-28
      相关资源
      最近更新 更多