Angular指令不在ionic.js中工作

时间:2014-05-17 17:48:41

标签: javascript angularjs ionic-framework

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

离子模板:

<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>

控制器代码:

'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

1 个答案:

答案 0 :(得分:6)

从指令名称中删除-

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

根据restrict: 'E',您应该将元素放在html中:

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

example