简体   繁体   English

动态JQuery中的Angular.JS

[英]Angular.JS in dynamic JQuery

Again I... I have stuck. 再次,我。。。 I need to use Angular.JS in my project, but I can't run Angular in dynamic generated tags. 我需要在项目中使用Angular.JS,但是无法在动态生成的标签中运行Angular。

    <h1>Your albums</h1><hr/>
    <div ng-controller="AlbumsController">
        <ul>
            <li ng-repeat="album in albums">
                {{album.name}}
                <p>{{album.desc}}</p>
            </li>
        </ul>
    </div>

That's block, which generated dynamically through JQuery. 那是通过JQuery动态生成的块。

<html ng-app="Application" class="no-js" lang="ru">

That's tag static, and it must init angular. 那是静态的标签,它必须初始化成角度。

My dynamic block doesn't work. 我的动态块不起作用。 It returns: {{album.name}} {{album.desc}}, that's sign of not initialized Angular.JS :( 它返回:{{album.name}} {{album.desc}},这表示未初始化 Angular.JS :(

var Juster = angular.module('Juster', []); 
Juster.controller('AlbumsController', function ($scope) {    
    $.post("/index.php/profile_ctrl/myalbums", function(data){ 
        $scope.albums = data; 
    }) 
});

Matt is correct in saying that you need to let go of jQuery for the most part when using angular. 马特的说法正确,就是在使用angular时,您需要放开jQuery的大部分时间。 It can cause some headaches when working with controllers and directives. 使用控制器和指令时,可能会引起一些麻烦。

To use only angular in your example I would use the $http service like so: 要在您的示例中仅使用angular,我将使用$ http服务,如下所示:

Juster.controller('AlbumsController', function ($scope, $http) { 

    $http({method:'GET', url: '/index.php/profile_ctrl/myalbums'})
        .success(function(data, status, headers, config) {
           $scope.albums = data; 
        })
        .error(function(data, status, header, config) {
           // Handle error
        });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM