星空网 > 软件开发 > Java

AngularJS中使用$parse或$eval在运行时对Scope变量赋值

 

在"AngularJS中自定义有关一个表格的Directive"中自定义了一个有关表格的Direcitve,其表格的表现方式是这样的:

 

<table-helper datasource="customers" clumnmap="[{name: 'Name'}, {street: 'Street'}, {age: 'Age'}, {url: 'URL', hidden: true}]"></table-helper>

以上,变量colmnmap的值是事先定义在了Scope中的:

 

return {  restrict: 'E',  scope: {    columnmap: '=',    datasource: '='  },  link:link,  template:template};

 

AngularJS中,还有一种运行时给Scope变量赋值的办法,那就是在link函数中使用$parse或$eval方法。

在Direcitve的呈现方面和以前一致:

<table-helper-with-parse datasource="customers" columnmap="[{name: 'Name'},...]"></table-helper-with-parse>

Directive大致是这样:

 

var tableHelperWithParse = function($parse){  var template = "",  link = function(scope, element, attrs){    var headerCols = [],      tableStart = '<table>',      tableEnd = '</table>',      table = '',      visibleProps = [],      sortCol = null,      sortDir = 1,      columnmap = null;      $scope.$watchCollection('datasource', render);            //运行时赋值columnmap      columnmap = scope.$eval(attrs.columnmap);            //或者      columnmap = $parse(attrs.columnmap)();      wireEvents();      function rener(){        if(scope.datasource && scope.datasourse.length){          table += tableStart;          table += renderHeader();          table += renderRows() + tableEnd;          renderTable();        }      }  };    return {    restrict: 'E',    scope: {      datasource: '='    },    link: link,    template: template  }}angular.module('direcitvesModule')  .directive('tableHelperWithParse', tableHelperWithParse);

 




原标题:AngularJS中使用$parse或$eval在运行时对Scope变量赋值

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

关税加征:https://www.goluckyvip.com/tag/14690.html
跨境交易提现:https://www.goluckyvip.com/tag/14691.html
GSP普惠待遇:https://www.goluckyvip.com/tag/14692.html
税务影响:https://www.goluckyvip.com/tag/14693.html
PayPal退款:https://www.goluckyvip.com/tag/14696.html
兰亭集势发展:https://www.goluckyvip.com/tag/14697.html
浏阳市里面哪里好玩的地方 浏阳市内有什么好玩的地方:https://www.vstour.cn/a/404233.html
你心目中的十大旅游景点有哪些?:https://www.vstour.cn/a/404234.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流