我有以下的JavaScript變量:更改代碼庫
scope.model = {
errors: {
email: ["error1", "error2"],
name: ["error"]
}
}
而且我有一個函數如下:
function (scope, element, attributes) {
if (scope.model.errors) {
if (scope.model.errors[attributes.validator])
// Do something
}
的問題是,這些錯誤並不總是同一範圍變量。
我能有這樣的事情:
scope.view = {
newErrors: {
email: ["error1", "error2"],
name: ["error"]
}
在函數內部我知道如何讓他們在哪裏變量:
function (scope, element, attributes) {
var errors = attributes["validatorErrors"];
// Note: errors is this case "view.newErrors"
// So the following code would become:
if (scope.view.newErrors) {
if (scope.view.newErrors[attributes.validator])
// Do something
}
UPDATE
我已經試過[]之前,但現在我明白了爲什麼它不工作:
function (scope, element, attributes) {
var errors = attributes["validatorErrors"];
if (scope[errors]) {
if (scope.[errors][attributes.validator])
// Do something
}
如果錯誤= '錯誤',它會工作...
如果錯誤= 'model.errors' 不會。在這種情況下,我需要:
scope['model']['errors'] ...
我該如何解決這個問題?
我不知道你在問什麼。 –
你的意思是你想動態獲取一個對象的屬性?如果是這樣,請使用[]符號來訪問它,即'scope [errors]',[property accessors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors ) –
當錯誤=「model.errors」時,它將不起作用。我會工作,如果錯誤=「錯誤」。查看我最近的更新。如何解決這個問題? –