Javascript 角度双向绑定未按预期工作

Javascript 角度双向绑定未按预期工作,javascript,angularjs,angularjs-directive,Javascript,Angularjs,Angularjs Directive,这是我的梦想值绑定到范围变量(会话号),而更改是一个获取当前会话号并执行一些检查的函数 return { restrict: 'A', scope: { value: '=value',change:'&change' }, template: '<a href="javascript:;" class="counter-minus" ng-click="minus()">-<

这是我的梦想<代码>值绑定到范围变量(会话号),而更改是一个获取当前会话号并执行一些检查的函数

return {
                restrict: 'A',
                scope: { value: '=value',change:'&change' },
                template: '<a href="javascript:;" class="counter-minus" ng-click="minus()">-</a>\
                          <input type="text" class="counter-field" ng-model="value" ng-change="changed()" ng-readonly="readonly">\
                          <a  href="javascript:;" class="counter-plus" ng-click="plus()">+</a>',

        link: function( scope , element , attributes ) {
                        // Make sure the value attribute is not missing.
                        if ( angular.isUndefined(scope.value) ) {
                            throw "Missing the value attribute on the counter directive.";
                        }

                        var min = angular.isUndefined(attributes.min) ? null : parseInt(attributes.min);
                        var max = angular.isUndefined(attributes.max) ? null : parseInt(attributes.max);
                        var step = angular.isUndefined(attributes.step) ? 1 : parseInt(attributes.step);

                        element.addClass('counter-container');

                        // If the 'editable' attribute is set, we will make the field editable.
                        scope.readonly = angular.isUndefined(attributes.editable) ? true : false;

                        /**
                         * Sets the value as an integer.
                         */
                        var setValue = function( val ) {

                            scope.value = parseInt( val );                    
                            scope.change();


                        }
                        setValue(scope.value + 1);
          }

     }
返回{
限制:“A”,
作用域:{value:'=value',change:'&change'},
模板:'\
\
',
链接:功能(范围、元素、属性){
//确保没有缺少value属性。
如果(角度未定义(范围值)){
抛出“计数器指令上缺少value属性。”;
}
var min=angular.isUndefined(attributes.min)?null:parseInt(attributes.min);
var max=angular.isUndefined(attributes.max)?null:parseInt(attributes.max);
var step=angular.isUndefined(attributes.step)?1:parseInt(attributes.step);
元素addClass('counter-container');
//如果设置了“可编辑”属性,我们将使字段可编辑。
scope.readonly=angular.isUndefined(attributes.editable)?true:false;
/**
*将值设置为整数。
*/
var setValue=函数(val){
scope.value=parseInt(val);
scope.change();
}
设置值(scope.value+1);
}
}

我想知道为什么
scope.change()
scope.value
之前执行。因为在
change()
内部,我使用了绑定到
scope.value
session\u number
,但总是
session\u number
有一个旧值。就像执行
scope.change()
时,
scope.value
尚未更改会话号变量一样

多亏了这个答案,我知道问题出在哪里了:

使用原语值的双向投标无法按预期工作

作用域继承通常是直接的,而您通常不这样做 甚至需要知道它正在发生。。。直到您尝试双向数据绑定 (例如,表单元素、ng模型)转换为基元(例如,数字、字符串、, 布尔)从子作用域内部在父作用域上定义


所以我需要将
value
绑定从原语值更改为对象

val
from
setValue(val)
在哪里定义的?
value
不会被取消定义吗?你是说你想要
scope.value
?@OmriAharon完成了,我忘了添加scope.Ok:)你的问题是,只有在执行
parseInt
之后,你才用相同的值重写
scope.value
。但是如果它是一个数字,它是相同的值。嗯,不是,这不是问题,我只是分配它。