简体   繁体   English

使用动态生成的链接动态更新html

[英]Dynamically updating html with dynamically generated links

My app generates a list of words and turns those words into links. 我的应用程序生成单词列表,并将这些单词转换为链接。 I want the user to be able to click those links and use the word as a search term. 我希望用户能够单击这些链接并将该词用作搜索词。 How do I retrieve the {{word.name}} from the HTML link? 如何从HTML链接中检索{{word.name}}? I don't want it to generate a new page if I click on the link. 如果我单击链接,则不希望它生成新页面。 I just want to be able to retrieve the text in the link. 我只希望能够检索链接中的文本。

HTML: HTML:

  <li>
      <a ng-href="...">{{ word.name }}</a> 
  </li>

Controller.js Controller.js

$scope.search = function() {
    $http.post('*', $scope.formData)
        .success(function(data) {
            $scope.formData     = {}; 
            $scope.word        = data;
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};

Node.js server code: Node.js服务器代码:

  app.get('*', function(req, res) 
  {
      //get the {{ word.name }} from the HTML and use it to search
      //sql = select.....+word.name;
      connection.query(sql, function (err, result, fields) 
      {
         if(err) throw err;
         res.json(result);
      });
  });

You need to put the word on the form's post data. 您需要在表格的过帐数据上加上文字。

When you make a post request you post some data on the server. 发出发布请求时,您会在服务器上发布一些数据。 You usually you do something like: 通常,您会执行以下操作:

<form action="yourfile.php" method="post">
    <input type="text" name="key_word" value="Search a word ...">
    <input type="submit" value="Search">
</form>

This will post to yourfile.php the data key_word=theValueOfInput 这会将数据key_word = theValueOfInput发布到yourfile.php

to do so with javascript use: 使用javascript来做到这一点:

<form id="myForm" action="yourfile.php" method="post">
   <input type="text" id="key_word" name="key_word" value="Search a word ...">
   <input type="submit" value="Search">
</form>
<script>
   function postMe(word){
      document.getElementById("key_word").value=word;
      document.getElementById("myForm").submit();
   }
</script>

<a onclick="postMe(this.innerHTML)">yourWord1Here</a>
<a onclick="postMe(this.innerHTML)">yourWord2Here</a>
<a onclick="postMe(this.innerHTML)">yourWord3Here</a>
...

Example : http://jsfiddle.net/Lyudj/2/ 范例: http//jsfiddle.net/Lyudj/2/

You can try 你可以试试

<li>
      <a href="#" ng-click="searchWord=word.name">{{ word.name }}</a> 
</li>

And on JS 然后在JS上

$scope.search = function() {
    $http.post('*', $scope.searchWord)
        .success(function(data) {
            $scope.formData     = {}; 
            $scope.word        = data;
        })
        .error(function(data) {
            console.log('Error: ' + data);
        });
};

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

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