2015-12-14 75 views
-1

控制器從請求中檢索數據,我必須用檢索到的數據更新html/input。對不起,問題是這個代碼不起作用,我沒有搞清楚。用控制器檢索到的數據修改html

exports.SearchBarController = function($scope, $http) { 
 
    
 
    $scope.update = function() { 
 
    var query = encodeURIComponent($scope.searchText); 
 

 
    $http. 
 
     get('/api/v1/product/text/' + query). 
 
     success(function(data) { 
 
     $scope.results = data.products; 
 
     }); 
 
    }; 
 

 
    setTimeout(function() { 
 
    $scope.$emit('SearchBarController'); 
 
    }, 0); 
 
};
<div class="search-bar-wrapper"> 
 
    <div ng-controller="SearchBarController"> 
 
     <input type="text" class="search-bar-input" ng-model="data.products" ng-  change="update()"> 
 
     <div class="product" ng-repeat="product in {{data.products}}"> 
 
     </div> 
 
    </div>

+4

什麼是你的問題?你看到什麼錯誤?你想要什麼? – tyler

+1

你想做什麼? 'data.products'綁定到一個文本字段,即只有一個值,並且您正在嘗試遍歷它。除此之外,您還可以在data.products中使用'product,而不是'{{data.products}}' – Tom

+0

它應該在輸入字段中輸入http請求中的數據,然後使用ngrepeat顯示結果。 – Walentes

回答

-1

exports.SearchBarController = function($scope, $http) { 
 
    
 
    $scope.update = function() { 
 
    var query = encodeURIComponent($scope.searchText); 
 

 
    $http. 
 
     get('/api/v1/product/text/' + query). 
 
     success(function(data) { 
 
     $scope.results = data.products; 
 
     }); 
 
    }; 
 

 
    setTimeout(function() { 
 
    $scope.$emit('SearchBarController'); 
 
    }, 0); 
 
};
<div class="search-bar-wrapper"> 
 
    <div ng-controller="SearchBarController"> 
 
     <input type="text" class="search-bar-input" ng-model="searchText" ng-change="update()"> 
 
     <div class="product" ng-repeat="product in results"> 
 
     </div> 
 
    </div>

+0

感謝您的文章做了一些澄清,但它仍然無法正常工作。 – Walentes

+0

其實只有ngcontroller div讓我陷入麻煩根據我的MVC,非常感謝。 – Walentes

+0

沒問題,不客氣。我只是想讓你弄清楚這個問題。 – dipesh

相關問題