Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将作用域值保存在另一个作用域中并同时显示_Javascript_Angularjs - Fatal编程技术网

Javascript 将作用域值保存在另一个作用域中并同时显示

Javascript 将作用域值保存在另一个作用域中并同时显示,javascript,angularjs,Javascript,Angularjs,我也是新来的AngularJS,有一个简单的问题 我想将中的值存储到另一个名为$scope.cde的作用域中,并同时显示它们。此时,仅显示{{abc},而不显示{{cde} 为什么这不适用于此代码 HTML: 使用ng change执行此操作 <input type="text" ng-model="abc" ng-change='cde = abc'> 使用ng change执行此操作 <input type="text" ng-model="abc" ng-change=

我也是新来的AngularJS,有一个简单的问题

我想将
中的值存储到另一个名为
$scope.cde的作用域中,并同时显示它们。此时,仅显示
{{abc}
,而不显示
{{cde}

为什么这不适用于此代码

HTML:


使用
ng change
执行此操作

<input type="text" ng-model="abc" ng-change='cde = abc'>

使用
ng change
执行此操作

<input type="text" ng-model="abc" ng-change='cde = abc'>

如果需要,您可以使用带有隔离作用域的指令

html

<body >
 <div ng-controller="ExampleCtrl">
  <h2>Scope Example</h2>
  <input type="text" ng-model="abc" placeholder="enter stuff here" x-change:scope="cde">
  <p><strong>abc scope:</strong> {{ abc }}</p>
  <p><strong>cde scope:</strong> {{ cde }}</p>
 </div>
</body>

检查一下

如果需要,可以将指令与隔离作用域一起使用:

html

<body >
 <div ng-controller="ExampleCtrl">
  <h2>Scope Example</h2>
  <input type="text" ng-model="abc" placeholder="enter stuff here" x-change:scope="cde">
  <p><strong>abc scope:</strong> {{ abc }}</p>
  <p><strong>cde scope:</strong> {{ cde }}</p>
 </div>
</body>

检查一下

安德烈的答案是正确的,除了,我会在函数中赋值
ng change=assignValue()
然后
assignValue
将是
$scope.cde=$scope.abc
@frishi谢谢,这正是我想要的。安德烈的答案是正确的,除了我会在函数中赋值
ng change=assignValue()
然后
assignValue
将是
$scope.cde=$scope.abc
@frishi谢谢,这正是我想要的。
myApp.directive('changeScope', function () {

  var controller = ['$scope', function ($scope) {
    $scope.setValue= function(abc){
      $scope.cde = abc;
    }
  }];

  var component = function(scope, element, attrs) {       
    scope.$watch('abc', function (v) {
      scope.setValue(v);
    });
  }

  return {
    link:       component,
    controller: controller,
    // Isolated scope
    scope:  {
      abc:  '=ngModel',
      cde: '=changeScope'
    }
  };

});