javascript - Onclick function inside a-href tag in angular controller calling method in same controller -


i loading data controller , displaying in view list of clickable items in html. when selected want call method in controller perform logic.

 items.push("<li><a href='#' ng-click='choosepostcode(" + result.postcode + ");return false;'>" + result.postcode + "</a></li>"); 

this displays correctly on view when clicked choosepostcode method not hit. have tried few alternatives no success.

'<li><a href="#" ng-click="alert("test");"> click </a></li>'  '<li><a href="#" ng-click="javascript:alert("test");"> click </a></li>'      "<li><a href='#' onclick="choosepostcode(result.postcode);return false;"> + result.postcode + </a></li>"); 

latest attempt comments below. still no success.

  <div ng-app="app" ng-controller="propertycontroller">                             <div class="input-group">                                 <input name="s" class="form-control" ng-model="addr" type="text" placeholder="search..." size="40" />                                 <span class="input-group-btn">                                     <button class="btn btn-primary" type="button" value="send" ng-click="addr_search()">search</button>                                 </span>                              </div>                             <div id="search">                                      <ul>                                         <li ng-repeat="result in results">                                             <a href="#" ng-click="$event.preventdefault();choosepostcode(result.postcode)">{{result.postcode}}</a>                                         </li>                                     </ul>                                                              </div> 

controller class.js

 function markerdrop() {                     $.getjson('https://api.postcodes.io/postcodes?lon=' + $scope.userlongitude + '&lat=' + $scope.userlatitude, function (data) {                                                  if (data.result !== null) {                             $scope.results = data.result;                         }                     });                 } 

i assuming case comment. try this,

template:

... <ul>    <li ng-repeat="result in results">       <a href="#" ng-click="$event.preventdefault();choosepostcode(result.postcode)">{{result.postcode}}</a>    </li> </ul> ... 

controller:

... $scope.globalpostcode = ''; $scope.choosepostcode = function(postcode) {     $scope.globalpostcode = postcode;  }; ... function markerdrop() {     $http.get('https://api.postcodes.io/postcodes?lon=' + $scope.userlongitude + '&lat=' + $scope.userlatitude)     .then(function(result) {                                 $scope.results = result.data || [];     }); } ... 

Comments

Popular posts from this blog

How to understand 2 main() functions after using uftrace to profile the C++ program? -

c# - Update a combobox from a presenter (MVP) -

How to put a lock and transaction on table using spring 4 or above using jdbcTemplate and annotations like @Transactional? -