简体   繁体   English

根据angularJS中的范围变量更改按钮文本

[英]Change button text based on scope variable in angularJS

I have a button in my page and its text (OFF / ON) will changes based on a variable in controller. 我的页面上有一个按钮,其文本(OFF / ON)将根据控制器中的变量而变化。 By default the variable value is false, So text will be ON. 默认情况下,变量值为false,因此文本将为ON。 When user click this button, variable value will be true and text will be OFF. 当用户单击此按钮时,变量值将为true,文本将为OFF。

Through another one button am opening a popup window, there I have this same button. 通过另一个按钮打开一个弹出窗口,我有这个相同的按钮。 This window will use the same controller. 该窗口将使用相同的控制器。

Issue is, if I change button text by clicking on it that same should reflect in popup window. 问题是,如果我通过单击更改按钮文本,则该文本应反映在弹出窗口中。 and changes in popup window should reflect in main page. 弹出窗口中的更改应反映在主页上。

.controller('myController', ['$scope', 'myService',
 function ($scope, myService) {
   $scope.btnText = myService.getTextState();
 };

$scope.toggleText = function(){
            $scope.btnText = myService.toggleText($scope.toggleText);;  
        };

 $scope.getTitle = function() {
            if($scope.btnText){
                return "OFF";
            }
            else{
                return  "ON";
            }
        };
]);



.service('plotPanelService',  function () {
 var btnText = false;

  toggleText: function (param) {
                btnText = !param;
                return (!param);
            },
  setTextState: function (paramText) {
                btnText = paramText;
            },

            getTextState: function () {
                return btnText;
            }
}

{{myVar === "true" ? {{myVar ===“ true”? "it's true" : ""}} “这是真的” : ””}}

if you want share model between two different controller, then you should create a service or factory. 如果要在两个不同的控制器之间共享模型,则应创建服务或工厂。

You are saying that, you are using same controller multiple times. 您说的是,您多次使用同一控制器。 Controllers are not singleton. 控制器不是单例。 It will be created multiple times. 它将被创建多次。 But factory or services are singleton. 但是工厂或服务是单例的。 So you can share same object between two controllers. 因此,您可以在两个控制器之间共享同一对象。

Another solution is create wrapper of your model. 另一个解决方案是为模型创建包装器。 If you create wrapper object in parent controller then you will have access of that directly in your child controller. 如果在父控制器中创建包装对象,则可以直接在子控制器中访问该包装对象。 Angular providers prototype inheritance. Angular提供程序原型继承。 So you will have same object in your child controller. 因此,您的子控制器中将具有相同的对象。

Checkout following plnkr plnkr之后结帐

var app = angular.module('plunker', ['uiSwitch']);

app.controller('controller', function($scope, myservice) {
  $scope.model = myservice.model;

});

app.service('myservice', function () {

  this.model = {
    enabled: false
  }
}); 

HTML : HTML:

  <div  ng-controller="controller">
    <p>Controller 2</p>
    <switch id="enabled" name="enabled" ng-model="model.enabled" class="green"></switch>
  <br>{{ model.enabled }}
    </div>
    <div  ng-controller="controller">
    <p>Controller 2</p>
    <switch id="enabled" name="enabled" ng-model="model.enabled" class="green"></switch>
  <br>{{ model.enabled }}
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM