gpt4 book ai didi

javascript - Angular 指令在 ionic.js 中不起作用

转载 作者:行者123 更新时间:2023-11-30 08:02:55 25 4
gpt4 key购买 nike

我在使用 Angular 将指令加载到我的 ionic.js 网络应用程序时遇到了一些麻烦。我可以传递变量 {{someTitle}}从 Controller 到模板,但是指令没有被加载,即 <div class="blaha" ng-transclude>abc123</div>没有传递给模板...我是否在下面遗漏了一些致命的东西:

ionic 模板:

<ion-view title="Roll the Dice">
<ion-content has-tabs="true">
<div my-dice>{{someTitle}}</div>

<div class="card">
<div class="item item-text-wrap">
{{someTitle}}
</div>
</div>


<button class="button button-dark button-full button-positive">
Full Width Block Button
</button>

</ion-content>
</ion-view>

Controller 代码:

'use strict';
angular.module('TrueDice.controllers', [])


// A simple controller that fetches a list of data from a service
.controller('HistoryIndexCtrl', function($scope, TrueRandomService) {

if($scope.rands="undefined"){
$scope.rands = [];
}
TrueRandomService.getRand(1).success(function(data){
$scope.rands.unshift(data);
console.log(data);
});
})
.controller('HistoryDetailCtrl', function($scope, $stateParams, TrueRandomService) {
//currently empty

}).controller('RollViewCtrl', function($scope){
$scope.someTitle="BLA";

}).directive('my-dice', ['$rootScope', function($rootScope) {
return {
restrict: 'E',
template: '<div class="blaha" ng-transclude>abc123</div>',
replace: true,
transclude: true,
scope: {},
controller: function($scope, $element) {
//currently empty
}
}
}]);

NotWorking

最佳答案

从指令名称中删除 -:

directive('myDice', ['$rootScope', function($rootScope) {

并且根据 restrict: 'E',您应该将 element 放在 html 中:

<my-dice>{{someTitle}}</my-dice>

example

关于javascript - Angular 指令在 ionic.js 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23714382/

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