可以使用動態參數並設置其範圍值?AngularJS - 動態範圍字段
$scope.testFunc= function(fieldName){
$scope.fieldName = 'Test';
}
可以使用動態參數並設置其範圍值?AngularJS - 動態範圍字段
$scope.testFunc= function(fieldName){
$scope.fieldName = 'Test';
}
var app = angular.module('myApp', []);
function myCtrl($scope, $parse) {
$scope.coolform = {};
$scope.testFunc = function(fieldName){
var model = $parse(fieldName);
model.assign($scope, 'test2');
}
}
,或者您可以使用自己的指令,而不是NG-點擊,並使用=
這裏描述how to set an interpolated value in angular directive?
感謝您的幫助,這個解決方案很酷 – John
如果您需要嵌套這樣的:
fieldName.secondLevel
你可以使用:
$scope[fieldName][secondLevel]
或者你使用正則表達式分析器使用的角度。 (我不知道如何使用這一個...)
編輯:
正則表達式從源代碼複製:
//000011111111110000000000022222222220000000000000000000003333333333000000000000004444444444444440000000005555555555555550000000666666666666666000000000000000777777777700000000000000000008888888888
var NG_OPTIONS_REGEXP = /^\s*([\s\S]+?)(?:\s+as\s+([\s\S]+?))?(?:\s+group\s+by\s+([\s\S]+?))?\s+for\s+(?:([\$\w][\$\w]*)|(?:\(\s*([\$\w][\$\w]*)\s*,\s*([\$\w][\$\w]*)\s*\)))\s+in\s+([\s\S]+?)(?:\s+track\s+by\s+([\s\S]+?))?$/
與用法:
var match;
if (!(match = optionsExp.match(NG_OPTIONS_REGEXP))) {
throw ngOptionsMinErr('iexp',
"Expected expression in form of " +
"'_select_ (as _label_)? for (_key_,)?_value_ in _collection_'" +
" but got '{0}'. Element: {1}",
optionsExp, startingTag(selectElement));
}
var displayFn = $parse(match[2] || match[1]),
valueName = match[4] || match[6],
keyName = match[5],
groupByFn = $parse(match[3] || ''),
valueFn = $parse(match[2] ? match[1] : valueName),
valuesFn = $parse(match[7]),
track = match[8],
trackFn = track ? $parse(match[8]) : null,
// This is an array of array of existing option groups in DOM.
// We try to reuse these if possible
// - optionGroupsCache[0] is the options with no option group
// - optionGroupsCache[?][0] is the parent: either the SELECT or OPTGROUP element
optionGroupsCache = [[{element: selectElement, label:''}]];
如您所見,他們使用$ parse來獲取所選表達式的值,如fieldName.secondLevel
感謝您的幫助,您的第一個解決方案($ scope [fieldName] [secondLevel])對我來說是正確的,並且工作正常。 – John
您是否嘗試過'$ scope.testFunc =函數(字段名){$範圍[字段名] ='測試'; ''? –