加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

angularjs – 如何从angular指令获取数据

发布时间:2020-12-17 07:17:19 所属栏目:安全 来源:网络整理
导读:我希望有人可以用一个小例子来帮助我,因为棱角分裂让我疯狂:( 我是第一次制作应该遵循这种结构的Formular: 角度APP mainController —- smallController1 ——– otherElements —- smallController2 ——– otherElements —- Directive1(实例1) —- anoth
我希望有人可以用一个小例子来帮助我,因为棱角分裂让我疯狂:(

我是第一次制作应该遵循这种结构的Formular:
角度APP
mainController
—-> smallController1
——–> otherElements
—-> smallController2
——–> otherElements
—-> Directive1(实例1)
—-> anotherSmallController
—-> Directive1(实例2)

Directive1接收许多属性,每个实例将允许选择许多选项,并且用户交互的结果应该存储在一个对象中,并且该对象应该分别从mainController为每个实例访问.

有没有人有这样的例子?

提前致谢,
约翰

从指令获取数据的最佳方法是将模型附加到指令的自我范围.
var app = angular.module('app',[]);

app.controller('mainController',[
  '$scope',function($scope){
    $scope.myObj = "Initial Value";
  }
]);

app.directive('dirName',[
  function(){
    return {
      restrict : 'A',scope : {
        obj : "=ngModel"
      },link : function(scope,element,attrs){
        scope.obj = attrs.newValue;
      }
    };
  }
]);
<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-2.1.1.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body ng-app="app" ng-controller="mainController">
  <input dir-name ng-model="myObj" new-value="Final Value">
</body>
</html>

您也可以检查此箱:http://jsbin.com/fuqujo/1/edit?html,output

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读