gpt4 book ai didi

javascript - 范围属性可见性

转载 作者:行者123 更新时间:2023-12-03 10:52:06 24 4
gpt4 key购买 nike

首先,我首先要说的是,我是 AngularJS 的新手,所以我的代码可能充满了反模式。如果是这样,请告诉我。

我正在使用 Angular-schema-formAngular-translate 来翻译我的标签。我已验证 $translate 服务返回的 Promise 已正确解析,但由于某种原因,$scope.labels 属性在 Promise 之外不可见。

我知道这可能是我对 Angular 内部工作原理的误解,但我无法确切地理解我做错了什么。

这是 Controller :

app = angular.module('myApp')

app.controller('SessionsController', ['$scope', '$translate',
($scope, $translate)->
$scope.labels = []

$translate(['models.user.labels.email', 'models.user.labels.password']).then(
(translations)->
$scope.labels = translations
)

# $scope.labels not visible here!

$scope.loginFormSchema = {
type: 'object',
properties: {
email: {
type: 'string',
title: $scope.labels['models.user.labels.email']
},

password: {
type: 'string',
title: $scope.labels['models.user.labels.password'],
'x-schema-form': {
type: 'password'
}
}
},
required: ['email', 'password']
}

$scope.loginForm = [
'*',
{
type: 'submit',
title: 'Sign in',
style: 'btn btn-lg btn-primary'
}
]

$scope.login = {}
])

最佳答案

$translate 是异步的。你可以这样做:

app = angular.module('myApp')

app.controller('SessionsController', ['$scope', '$translate',
($scope, $translate)->
$scope.loginFormSchema = {
type: 'object',
properties: {
email: {
type: 'string'
},

password: {
type: 'string',
'x-schema-form': {
type: 'password'
}
}
},
required: ['email', 'password']
}

$scope.loginForm = [
'*',
{
type: 'submit',
title: 'Sign in',
style: 'btn btn-lg btn-primary'
}
]

$scope.login = {}

$translate(['models.user.labels.email', 'models.user.labels.password']).then(
(translations)->
//$scope.labels = translations
$scope.loginFormSchema.properties.email.title = translations['models.user.labels.email'];
$scope.loginFormSchema.properties.password.title = translations['models.user.labels.password'];
)
])

关于javascript - 范围属性可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28394691/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com