【问题标题】:CSS/Javascript not binding on dynamic html appendCSS/Javascript 不绑定在动态 html 追加
【发布时间】:2017-06-30 07:36:08
【问题描述】:

我正在尝试从角度控制器动态绑定 html

SkillsApp.controller('homeController', function ($scope, $http, $q, $timeout) {
$scope.getAllCategories = function () {
    $http({
        url: "/Categories/GetAllCategories",
        method: 'GET',
    }).success(function (response) {
        $scope.categoriesList = response;
        for (var i = 0; i < $scope.categoriesList.length; i++)
        {
            var categoryyy = '<li><a href="#" data-filter=".commercial">' + $scope.categoriesList[i].Name + '</a></li>';                
            $('#Category').append(categoryyy);
        }           

    });
};

结果HTML:

<ol class="type" id="Category">                                
   <li><a href="#" data-filter=".commercial">Commercial</a></li>
  <li><a href="#" data-filter=".residential">Residential</a></li>
  <li><a href="#" data-filter=".commercial">Commercial</a></li>
  </ol>

目标HTML:

 <div class="col-sm-6 col-md-4 col-lg-4 RESIDENTIAL">
  <div class="portfolio-item">
   <div class="hover-bg">
    <a href="img/portfolio/01-large.jpg" title="Project Title" data-lightbox-gallery="gallery1">
     <div class="hover-text">
       <h4>Project Name</h4>
     </div>
     <img src="~/img/portfolio/01-small.jpg" class="img-responsive" alt="Project Title">
   </a>
  </div>
  </div>
  </div>

但是上面的代码没有绑定到目标元素。

如果是静态 html 代码,同样的代码可以完美运行。

请在我做错的地方帮助我。

更具体地说: 由于 html 的动态绑定,DOM 无法绑定 data-filter 动态 html 绑定后是否有刷新 DOM 对象的方法?

【问题讨论】:

  • 这不是实现你想要的正确角度的方式。看看ng-repeatdocs.angularjs.org/api/ng/directive/ngRepeat
  • 我想这里与角度无关,我会得到相同的结果,再次绑定失败。代替数据过滤器,href 将起作用。

标签: javascript jquery html css angularjs


【解决方案1】:

试着放一个sn -p这段代码

$scope.categoriesList = [];

因为您应该将数据收集到 $scope 内的数组中。顺便说一句,如果你想编码更清洁,你可以使用服务或工厂。这是LINK

尝试从角度看服务和工厂的不同。干杯

【讨论】:

    【解决方案2】:

    要循环进入数组,我建议使用ng-repeat 指令查看ng-repeat documentation

    AngularJS 不是 Jquery 请参阅 this question 以更好地了解 angularJS 的工作原理

    还有:
    您是否使用 ng-app="SkillsApp" 设置您的应用程序
    您是否使用ng-controller="homeController" 设置您的控制器
    你在某处打电话给getAllCategories() 吗?例如:ng-init="getAllCategories()"

    示例

    SkillsApp.controller('homeController', function ($scope, $http, $q, $timeout) {
    $scope.getAllCategories = function () {
        $http({
            url: "/Categories/GetAllCategories",
            method: 'GET',
        }).success(function (response) {
            $scope.categoriesList = response;
        });
    };
    
    
    <body ng-app="SkillsApp" ng-controller="homeController" ng-init="getAllCategories()" >
        <ol class="type"> 
            <li ng-repeat="categorie in categoriesList">
                <a href="#">{{categorie.name}}</a>
            </li>
        </ol>
    </body>
    

    【讨论】:

    • 我想这里与角度无关,我会得到相同的结果,再次绑定失败。代替数据过滤器,href 将起作用。
    • data-filter 应该做什么? angularjs 中没有 data-filter 内置指令
    猜你喜欢
    • 2013-03-03
    • 1970-01-01
    • 2018-01-06
    • 1970-01-01
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多