javascript - angularjs变量赋值
PHP中文网
PHP中文网 2017-04-10 17:53:23
[JavaScript讨论组]
 $scope.searchMsg={
     empName:$scope.search.name,
     empNum:$scope.search.num,
     startTime:$scope.search.startTimeValue,
     endTime:$scope.search.endTimeValue
         }

后边的$scope.search是一段封装的指令,大概类似这样

<lm-inputname></lm-inputname>
function lmInput_name() {
  return {
    restrict: 'E',
    scope: false,
    templateUrl: 'components/directiveTmp/lmInput_name.html',
    controller: function ($scope) {

    }
  }
}

下面是lmINput_name.html

<p ng-class="{true:'{{_init.nameCol}}',false:'col-sm-4'}[_init.nameCol!=undefined]">
  <p class="form-group">
    <label class="control-label">{{_init.name}}</label>
    <input  class="form-control" type="text" size="16" ng-model="search.name"
            placeholder="请输入{{_init.name}}">
  </p>
</p>

前面的$scope.searchMsg每次的属性可能不一样。
第一种:
想问的是有什么更好的方法给$scope.searchMsg赋值,看起来更优雅。
第二种:
或者可以提供一个方法在<lm-inputname set-model="来代替search.name"></lm-inputname>上再添加一个set-model的属性指令,可以每次动态定义不同的ng-model来代替search.name。
能做到第二种更好,初学者,望指教。

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
迷茫
<lm-inputname search-model="search.name"></lm-inputname>
function lmInput_name() {
  return {
    restrict: 'E',
    scope: {
        searchModel:'='  // =双向绑定,@绑定字符串,&绑定表达式
    },
    templateUrl: 'components/directiveTmp/lmInput_name.html',
    controller: function ($scope) {

    }
  }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号