AngularJS服务绑定值 [英] Angularjs binding value from service

查看:67
本文介绍了AngularJS服务绑定值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在一个或多个控制器之间共享服务值(在下面的示例中仅一个,但这不是重点).

I wish to share a service value between one or more controllers (only one in the following example but that's not the point).

问题在于服务中保留的值未绑定并显示在视图中.

The problema is that the value hold in the service is not bound and shown in the view.

代码(源自angularjs基本服务示例)为:

The code (derived from angularjs basic service example) is:

(function(angular) {
    'use strict';
angular.
module('myServiceModule', []).
    controller('MyController', ['$scope', 'notify','$log', function($scope, notify, $log) {
    $scope.callNotify = function(msg) {
        notify.push(msg);
    };

    $scope.clickCount = notify.clickCount();
    $log.debug("Click count is now", $scope.clickCount);
    }]).
factory('notify', ['$window','$log', function(win,$log) {
    var msgs = [];
    var clickCounter = 0;
    return {
        clickCount: function() {
            clickCounter = msgs.length;
            $log.debug("You are clicking, click count is now", clickCounter);
            return clickCounter;
            },
        push: function(msg) {
                msgs.push(msg);
                clickCounter = msgs.length;
                $log.debug("Counter is", clickCounter);
                if (msgs.length === 3) {
                win.alert(msgs.join('\n'));
                msgs = [];
                }
            }
        }
    }]);

我希望计数器显示在页面上

I wish the counter to be displayed on page:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-services-usage-production</title>


<script src="//code.angularjs.org/snapshot/angular.min.js"></script>
<script src="script.js"></script>



</head>
<body ng-app="myServiceModule">
<div id="simple" ng-controller="MyController as self">
<p>Let's try this simple notify service, injected into the controller...</p>
<input ng-init="message='test'" ng-model="message" >
<button ng-click="callNotify(message);">NOTIFY</button>
<p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
</div>
<div>You have clicked {{clickCount}} times</div>
</body>
</html>

朋克车

更新:更正了一些小错误,如@SehaxX所建议的html和服务代码

UPDATE: corrected the trivial errors is html and service code as suggested by @SehaxX

推荐答案

首先,您的HTML错误.您的最后一个div不在Controller的div中,并且您不需要self.

First your HTML is wrong. Your last div is not in the div of Controller, and you dont need the self.

<body ng-app="myServiceModule">
   <div id="simple" ng-controller="MyController">
     <p>Let's try this simple notify service, injected into the controller...</p>
     <input ng-init="message='test'" ng-model="message" >
     <button ng-click="callNotify(message);">NOTIFY</button>
     <p>(you have to click {{3-self.clickCount}} times more to see an alert)</p>
     <div>You have clicked {{clickCount}} times</div>
  </div>    
</body>

在您的服务中,您还缺少回报:

Also in your service you are missing return:

clickCount: function() {
            clickCounter = msgs.length;
            $log.debug("You are clicking, click count is now", clickCounter);
            return clickCounter;
          },

在您的控制器中,您只需调用一次notify.clickCount(),因此您需要将其添加到方法中:

And in your controller you only once call the notify.clickCount() so you need to add it to the method:

$scope.callNotify = function(msg) {
      notify.push(msg);
      $scope.clickCount = notify.clickCount();
      $log.debug("Click count is now", $scope.clickCount);
    };

如果您愿意的话,这里还有一个工作正常的代码笔.但是然后在控制器中,您必须使用它而不是$ scope.

Here also a working code pen with "Controller as self" if you want. But then in controller you must use this and not $scope.

干杯

这篇关于AngularJS服务绑定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆