꽤 연구 한 후에, 그리고 following issue을 참조하여, 여기에 (만 외부 nodeLinkFn
작동하도록 수정은, 본질적으로, 각 코드) 양방향 바인딩을 허용 범위의 확장이다 :
angular.module('scopeBindExtention', [])
.run([ '$rootScope', '$parse', function($rootScope, $parse) {
var extended = angular.extend($rootScope, {});
extended.$bindTwoWay = function(scopeName, parentName) {
var scope = this,
parentScope = scope.$parent;
lastValue,
parentGet,
parentSet,
compare;
parentGet = $parse(parentName);
if (parentGet.literal) {
compare = angular.equals;
} else {
compare = function(a,b) { return a === b; };
}
parentSet = parentGet.assign || function() {
// reset the change, or we will throw this exception on every $digest
lastValue = scope[scopeName] = parentGet(parentScope);
throw new Error("Expression '" + parentName + "' is non-assignable!");
/*
throw $compileMinErr('nonassign',
"Expression '{0}' is non-assignable!",
parentName);
*/
};
lastValue = scope[scopeName] = parentGet(parentScope);
var unwatch = parentScope.$watch($parse(parentName, function parentValueWatch(parentValue) {
if (!compare(parentValue, scope[scopeName])) {
// we are out of sync and need to copy
if (!compare(parentValue, lastValue)) {
// parent changed and it has precedence
scope[scopeName] = parentValue;
} else {
// if the parent can be assigned then do so
parentSet(parentScope, parentValue = scope[scopeName]);
}
}
return lastValue = parentValue;
}), null, parentGet.literal);
scope.$on('$destroy', unwatch);
}
}]);
클라이언트는 다음을 호출해야합니다.
범위. $ bindTwoWay ('childModelName', 'parentModelName');
왜'obj.name = 'thename''을 넣고'my-attr = "obj"'를 사용합니까? –