Javascript 将作用域值保存在另一个作用域中并同时显示
我也是新来的AngularJS,有一个简单的问题 我想将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=
中的值存储到另一个名为$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'
}
};
});