2
私のような問題に対する素晴らしい答えを見ていますが、まだ動作していない代替コードが見つからないか、コードが失敗しています。 私は実際に似たような問題を投稿した人物を実際に見つけましたが、彼に与えられた答えは私のために働いていません...そして私は自分自身の違いを見つけることができるように、 私はあなたが私に手を差し伸べることを望んでいたのですか?TypeError:mvcRoutes.calledは関数ではありませんが、関数として宣言しましたが
これはその後
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.jsですが、私は私のサービスがあります。
directives.service("countryService", ["$http", "mvcRoutes", "$q", function ($http, mvcRoutes, $q) {
return {
search: function (text) {
return $http.post(mvcRoutes.called("country.search"), { text: text});
}
};
}]);
and lastly, my directive:
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 }); } }] } }]);
私はまた私のコントローラは、このように、非常に単純に定義されています:
[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"; } }
しかし、私がそれを実行すると、私は彼のスタック:
TypeError: mvcRoutes.called is not a function
at Object.search (Directives.js:95)
at Scope.$scope.getCountries (Directives.js:113)
at Object.fn [as source] (eval at compile (angular.js:13322), <anonymous>:4:317)
at getMatchesAsync (ui-bootstrap-tpls.js:7224)
at Array.<anonymous> (ui-bootstrap-tpls.js:7451)
at NgModelController.$$parseAndValidate (angular.js:25256)
at NgModelController.$commitViewValue (angular.js:25246)
at angular.js:25383
at angular.js:17855
at completeOutstandingRequest (angular.js:5507)
私は間違って何をしているのか、どのように修正するのですか?
ありがとうございます!