我是安格拉斯的新人。目前正在进行服务工作。我试图将文本框值传递给服务。我不知道怎么过去。我编码但没用。

app.service("wishService", function ($timeout) {
    this.wishHim = function (_wishMessage) {
        $timeout(function () {
            alert('Hi ,' + _wishMessage);
        }, 2000);
    }
});

app.controller('wishCtrl', ['$scope', function ($scope, wishService) {
    $scope.Message = "";
    $scope.SendMessage = function (wishMessage) {
        wishService.wishHim(wishMessage);
    };
}]);


<div ng-controller="wishCtrl">
        Wish Message: <input type="text" ng-model="wishMessage" />
        <input type="button" value="Send Message" ng-click="SendMessage(wishMessage)" /><br />
        Your Message : {{Message}}
</div>

DEMO

最佳答案

wishService注入问题

app.controller('wishCtrl', ['$scope', 'wishService', function ($scope, wishService) {
    $scope.Message = "";
    $scope.SendMessage = function (wishMessage) {
        wishService.wishHim(wishMessage);
    };
}]);

10-04 16:51