【问题标题】:Angularjs directive not working as expectedAngularjs 指令未按预期工作
【发布时间】:2013-03-04 19:04:35
【问题描述】:

我对 angularjs 很陌生,所以这可能听起来微不足道。我想要完成的是最初在页面上显示一个图像和一个按钮。当用户单击按钮时,另一个图像也会出现在页面上。

这是我的html代码

    <div ng-app="test">
<hello>
    <pane>
    </pane>
</hello>
</div>

我的 Angular js 指令是

    angular.module('test', []).directive('hello', function() {
    return {
        restrict : 'E',
        template : '<div style="position: relative"><input name="Add" type="submit" ng-click="AddMarker()" ><img src="http://www.india-travelinfo.com/india-maps/india-map.jpg" /></div>',
        replace: true,
        link : function(scope, element, attrs) {
                 $("#mr").draggable();
        },
        controller: function($scope, $element) {
        var panes = $scope.panes = [];

        $scope.select = function(pane) {
          angular.forEach(panes, function(pane) {
            pane.selected = false;
          });
          pane.selected = true;
        }

        this.addPane = function(pane) {
          if (panes.length == 0) $scope.select(pane);
          panes.push(pane);
        }
      }
    };
}).
 directive('pane', function() {
    return {
      require: '^hello',
      restrict: 'E',
      transclude: true,
      scope: { title: '@' },
      link: function(scope, element, attrs, tabsCtrl) {
        tabsCtrl.addPane(scope);
      },
      template:
        '<img id="mr" class="drag-image" src="http://www.mbs.edu/i/gmap_marker_default.gif" style="position: absolute;" />',
      replace: true
    };
  })

谁能指出这个指令可能有什么问题。这是jsfiddle

【问题讨论】:

  • AddMarker() 是在哪里定义的?

标签: javascript jquery angularjs angularjs-directive


【解决方案1】:

实际上,我认为您对指令的理解太深了。您要构建的功能实际上只是直接的 Angular,不需要指令。试试这个(小提琴更新):

<div ng-app="test" ng-controller="mapController">
    <div style="position: relative">
        <button name="Add" type="button" ng-click="showMarker = true">Show Marker</button>
        <img src="http://www.india-travelinfo.com/india-maps/india-map.jpg" />
        <img id="mr" ng-show="showMarker" class="drag-image" src="http://www.mbs.edu/i/gmap_marker_default.gif" style="position: absolute;" />
    </div> 
</div>

控制器:

angular.module('test', [])
.controller('mapController', function ($scope) {

})

http://jsfiddle.net/SfFub/3/

【讨论】:

  • okk 数据看起来不错,刚接触 Angular js 我想我需要学习很多新东西。谢谢它的帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 1970-01-01
相关资源
最近更新 更多