AngularJS:将文本框值传递给服务

AngularJS: Pass textbox value to services

我是 angularJS 的新人。目前从事服务工作。我试图将文本框值传递给服务。我不知道如何通过。我编码,但它不工作。

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);
    };
}]);

我修复了您的代码,请参阅此处的 PLUNK。虽然 Sandeep 所说的是正确的,但您还有很多其他问题。参考我改的。

编辑:我注意到我将您的代码更改为工厂格式,而不是服务格式:

app.service("MessageService", function () {

var service = {
  msg: undefined,

  setMsg: function(msg){
    this.msg = msg; 
  },

  getMsg: function(msg){
    return this.msg; 
  }
}

return service;
});