我对angularJS很新。我想在我的一个文本框中使用typeahead,但是当我使用它时会出现错误:
TypeError:无法调用undefined的'replace'方法 在范围。 (http://XX.YY.ca/js/lib/ui-bootstrap.js:3426:32) 在fnInvoke(http://XX.YY.ca/js/lib/angular.js:10017:21) 在运营商。| (http://XX.YY.ca/js/lib/angular.js:9532:59) at Object.extend.constant [as get](http://XX.YY.ca/js/lib/angular.js:9962:14) 在Scope。$ digest(http://XX.YY.ca/js/lib/angular.js:11800:40) 在Scope。$ apply(http://XX.YY.ca/js/lib/angular.js:12061:24) 在HTMLButtonElement。 (http://XX.YY.ca/js/lib/angular.js:17839:21) 在HTMLButtonElement.n.event.dispatch(http://code.jquery.com/jquery-1.11.0.min.js:3:8066) 在HTMLButtonElement.r.handle(http://code.jquery.com/jquery-1.11.0.min.js:3:4767)
我的代码与angular-ui网站上的代码基本相同。这是:
标记:
<div class="modal-body">
<div class="form-group">
<input type="text" id="customer" autocomplete="off"
ng-model="nom" tabindex="0"
typeahead="customer.customerFirstName for customer in getCustomers($viewValue) | filter:$viewValue"
typeahead-wait-ms="300" typeahead-on-select="setId($item)"
typeahead-editable="false" class="form-control input-sm" placeholder="Customer" />
</div>
</div>
控制器:
[...]
$scope.getCustomers = function (val) {
return $http.get('/index.php/customers/get_customers_ajax', {
params: {val: val}
}).then(function (res) {
var customers = [];
angular.forEach(res.data, function (item) {
customers.push({
'customerID': item.customerID,
'customerFirstName': item.customerFirstName,
'customerLastName': item.customerLastName
})
})
console.log(customers);
return customers;
});
}
$scope.setID = function ($item) {
$scope.newOrder.customerID = $item.customerID;
Console.log($scope.newOrder.customerID);
}
});
后端响应:
[
{"CustomerID":"1","CustomerLastName":"Pan","CustomerFirstName":"Petter"},
{...}
]
我不知道我能提供什么附加信息,但如果有的话,请问我会发帖!
答案 0 :(得分:2)
我认为你应该改变这一行:
typeahead="customer.customerFirstName for customer in getCustomers($viewValue) | filter:$viewValue"
似乎语法应为:
typeahead="<name> for <name> in getCustomers($viewValue) | filter:$viewValue"
如果第一个位置的<name>
在第二个位置不等于<name>
,则会返回错误。
尝试将您的代码更改为:
typeahead="customer for customer in getCustomers($viewValue) | filter:$viewValue"
并在你的javascript中将customerFirstName
直接放入返回的数组中。
答案 1 :(得分:0)
我认为问题是你设置了ng-model =“nom”,但你没有在你的控制器中定义$ scope.nom变量。 Angular正试图将从typeahead中选择的客户绑定到控制器中的nom变量。