AngularJs从模型创建范围
我有一个复杂的模型结构(如下所述),每次我想使用AngularJs从模型创建范围,angularjs,angularjs-scope,Angularjs,Angularjs Scope,我有一个复杂的模型结构(如下所述),每次我想使用ng模型时,我都必须访问如下确切的属性:user.communications.inGame.selected 我希望能够确定一个区域的范围,并能够使用内部选定的,而不使用所有前缀(只需编写选定的),就像我能够使用ng repeat一样 ng repeat不适合这里,因为每个通信都有不同的属性,我不想在它里面有一个巨大的ng开关 数据结构 $scope.user.communications = { inGame: {
ng模型
时,我都必须访问如下确切的属性:user.communications.inGame.selected
我希望能够确定一个区域的范围,并能够使用内部选定的,而不使用所有前缀(只需编写选定的),就像我能够使用ng repeat
一样
ng repeat
不适合这里,因为每个通信都有不同的属性,我不想在它里面有一个巨大的ng开关
数据结构
$scope.user.communications = {
inGame: {
name: 'inGame',
selected: true,
image: 'assets/img/communication/ingame.png'
},
teamspeak: {
name: 'teamspeak',
selected: true,
image: 'assets/img/communication/ts.png',
serverAddress: '',
port: '',
nickname: '',
password: '',
channel: '',
channelPassword: '',
autoBookmarkAdd: ''
},
skype: {
id: 3,
name: 'skype',
selected: true,
image: 'assets/img/communication/skype.png',
username: ''
},
ventrilo: {
name: 'ventrilo',
selected: true,
image: 'assets/img/communication/ventrilo.png',
serverName: '',
port: '',
serverPassword: '',
channelName: '',
channelPassword: ''
}
};
请检查下面的html,你可以这样使用它
<div ng-app>
<h2>Todo</h2>
<div ng-controller="TodoCtrl">
<ul class="unstyled">
<li ng-repeat="todo in user.communications">
{{todo.selected}}
</li>
</ul>
</div>
</div>
}
请参阅:如果您想在单独选择$scope.user.communications
中的每个项目时动态创建作用域,只需使用ng init
。但是,它至少需要一个前缀
<div ng-model="one" ng-init="one=user.communications.inGame">
{{ one.selected }}
</div>
...
{{one.selected}}
...
是否可以编写自己的指令?类似于ng with
,然后执行ng with=“user.communications.inGame”
的操作,不确定这是否适用于您,因为这意味着模板需要包含在指令中。不过这只是一个想法。我想在不重复的情况下获得与ng repeat相同的效果,希望找到一个内置解决方案,否则我可能会尝试实现一个。我认为您至少需要一个前缀。我想不出Angular中已经有任何具有该功能的内容,这就是为什么我建议编写您自己的指令。我确实发现这可能会有所帮助,谢谢,但我已经提到ng repeat不适合这里,因为每个通信都有不同的属性,我希望能够在不使用ng开关的情况下访问其他属性
<div ng-model="one" ng-init="one=user.communications.inGame">
{{ one.selected }}
</div>
...