頗經過一番研究,並參照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);
}
}]);
客戶應該只需撥打電話:
scope。$ bindTwoWay('childModelName','parentModelName');
爲什麼只放'obj.name ='thename''並使用'my-attr =「obj」'? –