首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >范围属性可见性

范围属性可见性
EN

Stack Overflow用户
提问于 2015-02-08 13:53:34
回答 1查看 65关注 0票数 0

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

我正在使用angular-schema-formangular-translate翻译我的标签。我已经验证了$translate服务返回的承诺是正确的,但是由于某种原因,$scope.labels属性在承诺之外是不可见的。

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

这里是控制器:

代码语言:javascript
复制
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 = {}
])
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-02-08 14:11:41

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

代码语言:javascript
复制
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'];
    )
])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28394691

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档