我一直在寻找像我这样的问题的令人敬畏的答案,但仍然找不到可行的替代方案,或者为什么我的代码失败了。 我确实找到了一个发布了与我的问题非常相似的问题的人,但给他的答案对我来说也不起作用......而且我对js和我来说太新了,能够自己发现差异。 我希望你能伸出援助之手?
这是我的Directives.js
var directives = angular.module("directives", []);
directives.constant("mvcRoutes", {
items: {},
called: function (name) {
var value = this.items[name];
if (!value) throw "Could not find route for " + name + " make sure its realy added/defined through script tag!";
return value;
}
});
之后,我有我的服务:
directives.service("countryService", ["$http", "mvcRoutes", "$q", function ($http, mvcRoutes, $q) {
return {
search: function (text) {
return $http.post(mvcRoutes.called("country.search"), { text: text});
}
};
}]);
最后,我的指示:
directives.directive("countrySelector", ["countryService", "$rootScope", function (countryService, $rootScope) {
var myCtrl; return { restrict: "E", template: '<input type="text" id="{{::elementId}}" name="{{::elementId}}" class="form-control" autocomplete="off" ng-required="" ng-model="country" typeahead-select-on-blur="true" ng-maxlength="50" typeahead-select-on-exact="true" ng-model-options="{ debounce: 800, allowInvalid: true }" uib-typeahead="item.id as item.name for item in getCountries($viewValue)" typeahead-loading="loadingLocations" typeahead-no-results="noResults" typeahead-template-url="country-template.html" typeahead-show-hint="true" typeahead-min-length="1" typeahead-on-select="itemSelected($item, $model, $label, $event)">', scope: { elementId: "@elementId", country: "=ngModel", required: "=?ngRequired" }, link: function (scope, element, attr, ctrl) { myCtrl = ctrl; }, controller: ["$scope", function ($scope) { $scope.getCountries = function (text) { return countryService.search(text).then(function (response) { return response.data }); } if (!$scope.elementId) $scope.elementId = "country"; $scope.itemSelected = function ($item, $model, $label) { $scope.country = $label; $rootScope.$broadcast("countrySelector.selected", { id: $scope.elementId, label: $label }); } }] } }]);
var myCtrl; return { restrict: "E", template: '<input type="text" id="{{::elementId}}" name="{{::elementId}}" class="form-control" autocomplete="off" ng-required="" ng-model="country" typeahead-select-on-blur="true" ng-maxlength="50" typeahead-select-on-exact="true" ng-model-options="{ debounce: 800, allowInvalid: true }" uib-typeahead="item.id as item.name for item in getCountries($viewValue)" typeahead-loading="loadingLocations" typeahead-no-results="noResults" typeahead-template-url="country-template.html" typeahead-show-hint="true" typeahead-min-length="1" typeahead-on-select="itemSelected($item, $model, $label, $event)">', scope: { elementId: "@elementId", country: "=ngModel", required: "=?ngRequired" }, link: function (scope, element, attr, ctrl) { myCtrl = ctrl; }, controller: ["$scope", function ($scope) { $scope.getCountries = function (text) { return countryService.search(text).then(function (response) { return response.data }); } if (!$scope.elementId) $scope.elementId = "country"; $scope.itemSelected = function ($item, $model, $label) { $scope.country = $label; $rootScope.$broadcast("countrySelector.selected", { id: $scope.elementId, label: $label }); } }] } }]);
我也非常简单地定义了我的控制器,如下所示:
[RoutePrefix("{site}/country")] public class CountryController : Controller { // GET: Country [Route("search")] public ActionResult Search(string text) { using (var connection = CreateSqlConnection.ToRepairRequestDb()) { connection.Open();
return new CountryQuery(text).Execute(connection).AsJsonResult(); } } } public class CountryQuery : SqlQuery<CountryModel> { public CountryQuery(string text, int maxRows = 6) { Parameters = new { text = text.AsSqlWildcard(false), maxRows }; SqlText = @" SELECT TOP (@maxRows) CountryID AS ID, CountryName as Name FROM Countries WHERE (@text IS NULL OR CountryID LIKE @text OR CountryName LIKE @text) ORDER BY CountryName asc"; } }
但是当我运行它时,我得到了这个堆栈:
return new CountryQuery(text).Execute(connection).AsJsonResult();
}
}
}
public class CountryQuery : SqlQuery<CountryModel>
{
public CountryQuery(string text, int maxRows = 6)
{
Parameters = new { text = text.AsSqlWildcard(false), maxRows };
SqlText = @"
SELECT TOP (@maxRows) CountryID AS ID, CountryName as Name FROM Countries
WHERE
(@text IS NULL OR CountryID LIKE @text OR CountryName LIKE @text)
ORDER BY CountryName asc";
}
}
知道我做错了什么,以及如何解决它?
提前致谢!!
答案 0 :(得分:0)
我犯了一个严重错误......我的解决方案有两个directives.js文件;我写了一个错误的声明,属于一个不同的项目。 只要我将其移动到正确的代码,代码就可以正常运行。